zoukankan      html  css  js  c++  java
  • wpf的webbrowser与javascript交互

    JS调用C#代码

    HTML代码:

    <button onclick="window.external.Test('called from script code')">
        call client code from script code
    </button>
    

    WPF代码中的C#代码:

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }
    

    WPF和JavaScript之间的通信需要完全信任,因此您需要添加以下代码:

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]
    public class MainWindow
    {
        public void Test(String message)
        {
            MessageBox.Show(message, "client code");
        }
    }
    

    也经常这么干

    //webbrowser设置一个脚本回调的上下文
    mywebbrowser.objectForScription = nwe scriptCallBackContext();
    
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]
    public calss scriptCallBackContext
    {
    
        public void Test(String message)
        {
            MessageBox.Show(message, "client code");
        }
    }
    

    c#调用 JS方法

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string curdir = Directory.GetCurrentDirectory();
        webBrowser.Navigate(String.Format("file:///{0}/test.html", curdir));
        webBrowser.LoadCompleted += (ss, ee) =>
        {
            var jsCode = "fillData('data...');";
            dynamic doc = webBrowser.Document;
            webBrowser.InvokeScript("execScript", new Object[] { jsCode, "JavaScript" });
        };
    }
    

    myscripts.js:

    function fillData(data)
    {
        //document.getElementById("uname").value = data;
        var oVDiv = document.getElementById("uname");
        //oVDiv.setAttribute("vaue", data);
        oVDiv.value = data;
    
        //oVDiv.value = data;
        //document.write(data);
    }
    

    转载于:https://www.cnblogs.com/nocanstillbb/p/10417246.html

  • 相关阅读:
    浅谈Java设计模式——状态模式(State)
    浅谈Java设计模式——解释器模式(Interpreter)
    浅谈Java设计模式——备忘录模式(Memento)
    struts2 标签
    Struts2 Web资源获取
    链接
    Struts2数据封装机制
    struts学习
    位运算
    归并排序求逆序对
  • 原文地址:https://www.cnblogs.com/cnwy/p/13300038.html
Copyright © 2011-2022 走看看