CefSharp For WPF響應頁面點擊事件


初始化

<!--瀏覽器-->
<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方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM