CefSharp For WPF响应页面点击事件

时间:2017-12-07 13:17:43   收藏:0   阅读:860

初始化

<!--浏览器-->
<cefSharpWPF:ChromiumWebBrowser Name="webBrowser" 
                                Grid.Row="0" >
</cefSharpWPF:ChromiumWebBrowser>
public MainWindow()
{
    InitializeComponent();

    //在js里面注册对象bound,然后用该对象调用C#方法
    webBrowser.RegisterAsyncJsObject("bound", new BoundObject(this), BindingOptions.DefaultBinder); //Use the default binder to serialize values into complex objects
}

BoundObject.cs

public class BoundObject
{
    private Window mainWindow { get; set; }
    public BoundObject(Window _mainWindow)
    {
        mainWindow = _mainWindow;
    }

    /// <summary>
    /// 打开exe文件
    /// </summary>
    public void OpenFile()
    {
        try
        {
            //MessageBox.Show("(\"This is an MyMethod coming from C#\")", "haha");
            string path ="你要打开的文件目录";
            System.Diagnostics.Process.Start(path);
        }
        catch(Exception ex)
        {
            MessageBox.Show("请输入正确的文件路径!", "提示");
        }
    }
}

js调用

bound.openFile();

bound在C#中定义,这样就会调用BoundObject类的OpenFile方法

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!