WPF和js互動 呼叫窗體中的方法

yuejin發表於2014-08-29
public partial class WebTest: Window
{
   private void Window_ContentRendered(object sender, EventArgs e)
   {
     this.wbrExam.ObjectForScripting = new OprateBasic(this);
     this.wbrExam.Source = new Uri(Environment.CurrentDirectory + @"\res\template.html");
   }
   
   public void Wtest(string str)
   {
        MessageBox.Show(str);
   }
}

[System.Runtime.InteropServices.ComVisible(true)]
public class OprateBasic
{
    private WebTest instance;
    public OprateBasic(WebTest instance)
    {
       this.instance = instance;
    }
    
    public void HandleTest(string p)
    {
        instance.Wtest(p);
    }
}

js->cs:window.external.HandleTest(“測試”);

cs->js:this.wbrExam.InvokeScript("js方面名", new object[] { "hello" });

 

相關文章