捕捉鼠标点击事件

问题描述 投票:1回答:1

我真的一派这死亡,但没有找到任何有助于多。我使用C#WPF cefsharp.wpf cefSharp:ChromiumWebBrowser

所有我想要做的就是当过用户点击网页我要赶在C#中的鼠标点击事件上的任何东西。原因是我的JavaScript寄存器被点击了什么,并传回一个值的页面上运行。所以,我需要得到的JavaScript该值。一切都设置。我只是不能让C#来注册浏览器被点击。 C#犯规需要知道被点击网页上的内容元素,JavaScript的处理是对我来说。

任何帮助将不胜感激。

c# wpf cefsharp
1个回答
1
投票

如果您捕捉你的JavaScript单击事件得到什么页面,实际上,是您的活动上选择。在JavaScript调用从点击一个C#方法。

例如,在一个应用程序,我写我在CefSharp渲染的D3页面显示力布局图,当用户在力布局的JavaScript发送该项目的ID到C#这又回到一个序列化的项目点击片回JavaScript的数据,然后将其显示的(有效我有充当向和从JavaScript的CefSharp和我的模型处理序列之间的层的C#类)。这听起来像你需要采取这样的你想什么来实现,而不需要在浏览器上控制自身的实际点击事件的方法。

我已经添加低于一些示例代码示出了在屏幕上在CefSharp的按钮(如index.html中定义)。当点击了按钮,它传递从页到JsHandler类这又示出了具有值的信息框的值。

Mainwindow.xaml

<Window x:Class="CefSharpWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CefSharpWPF"
        xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <wpf:ChromiumWebBrowser Grid.Row="0"
                                x:Name="Browser" />
    </Grid>
</Window>

Mainwindow.cs

    public partial class MainWindow : Window
    {
        JsHandler jsHandler = new JsHandler();

        public MainWindow()
        {
            InitializeComponent();
            Browser.Address = Environment.CurrentDirectory + "/index.html";
            Browser.JavascriptObjectRepository.Register("jsHandler", jsHandler);
        }
    }

的index.html

    <html>
        <head>
            <script type="text/javascript">
                CefSharp.BindObjectAsync("jsHandler", "jsHandler");
            </script>
            <title>Test Page</title>
        </head>
        <body>
            <button onclick="window.jsHandler.handleJsCall(1)">button</button>
        </body>
    </html>

JsHandler.cs

    class JsHandler
    {
        public void HandleJsCall(int arg)
        {
            MessageBox.Show($"Value Provided From JavaScript: {arg.ToString()}", "C# Method Called");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.