WPF 中嵌入的浏览器不处理指针事件

And*_*erd 5 browser wpf touch winforms

我在 Windows 7 WPF 应用程序中托管 WebBrowser 控件。

现在我在该浏览器中运行的 javascript 遇到问题。DOMpointer事件没有触发。当我单击 DOM 对象时,mousedownclick事件会触发,但该pointerdown事件不会触发,即使在 Internet Explorer 11 中查看同一页面时会触发该事件。

如何触发 DOMpointerdown事件?

这是我在浏览器中看到的内容:

浏览器图像

这是我在 WPF 应用程序中看到的内容:

WPF图像

这是我正在测试的 HTML 文档:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=11">
    <script type="text/javascript" src="./scripts/jquery.min.js"></script>
    <title>Raw test page</title>
    <style type="text/css">
        #mouseTarget{
            border: 2px solid purple;
            background: steelblue;
            font-weight: bold;
            width: 300px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div id="mouseTarget">Mouse Target</div>
    <div id="logOutput"></div>
    <script type="text/javascript">
        var logOutput = function (text) {
            $("<div></div>").text(text).appendTo($("#logOutput"));
        };
        var mouseTarget = document.getElementById('mouseTarget');
        mouseTarget.addEventListener('pointerdown', function () {
            logOutput('pointerdown event received');
        }, false);
        mouseTarget.addEventListener('mousedown', function () {
            logOutput('mousedown event received');
        }, false);
        mouseTarget.addEventListener('click', function () {
            logOutput('click event received');
        }, false);
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Flo*_*kap 3

编辑:抱歉,该设置似乎不足以触发指针事件。它只是识别触摸事件,但仍然只触发鼠标事件。很烦人...


问题是 WebBrowser 控件的行为与通常的 IE 实例不同。首先,它默认使用旧版回退 IE7 模式。其他要点包括遗留输入模型等等。

我个人遇到了将浏览器模式设置为 IE10 的问题,但指针事件根本不起作用。问题是,WebBrowser 控件假装支持我订阅的 PointerEvents,但由于启用了旧输入模型,这些控件没有被触发。

您可以从应用程序内部动态地为该应用程序设置这些策略:

private void SetBrowserCompatibilityMode()
    {
        // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

        // FeatureControl settings are per-process
        var fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

        if (String.Compare(fileName, "devenv.exe", true) == 0) // make sure we're not running inside Visual Studio
            return;

        using (var key = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
            RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
            UInt32 mode = 10000; // 10000; or 11000 if IE11 is explicitly supported as well
            key.SetValue(fileName, mode, RegistryValueKind.DWord);
        }

        using (var key = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_NINPUT_LEGACYMODE",
            RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            // disable Legacy Input Model
            UInt32 mode = 0;
            key.SetValue(fileName, mode, RegistryValueKind.DWord);
        }

    }
Run Code Online (Sandbox Code Playgroud)

参考:C# WebBrowser PanningMode