最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to communicate between WPF and JavaScript in a WebBrowser instance? - Stack Overflow

programmeradmin6浏览0评论

I have a C#/WPF application with an embedded browser (WebBrowser) with some JavaScript. How can they municate with each other in both directions? Is it practicable to use the URL?

JS->WPF: Listen for changes. WPF->JS: Change the URL to javascript:alert('hello');

Is there a better way?

I have a C#/WPF application with an embedded browser (WebBrowser) with some JavaScript. How can they municate with each other in both directions? Is it practicable to use the URL?

JS->WPF: Listen for changes. WPF->JS: Change the URL to javascript:alert('hello');

Is there a better way?

Share Improve this question edited Jan 22, 2024 at 12:14 Jonas 129k101 gold badges327 silver badges405 bronze badges asked Aug 21, 2012 at 10:30 koalabruderkoalabruder 2,9049 gold badges35 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

It's important to know that there are two WebBrowser. One for Windows Forms and one for WPF. The link below uses the WPF one. http://www.dotnetfunda./articles/article840-working-with-webbrowser-in-wpf.aspx

To invoke JavaScript function from C#

object result = mWebBrowser.Document.InvokeScript("FunctionName", new String[] { Parameter1 });

// If the function succeed (my function return a boolean)
if (null == result || result.ToString().ToLower() == "false")
{
   // Code
}

Is this what you want? Like this your c# code call your javascript, and it returns a value

My WPF Xaml:

<UserControl x:Class="MediaViewer.WebViewer"
         xmlns="http://schemas.microsoft./winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft./winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats/markup-patibility/2006"
         xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
   <WindowsFormsHost>
      <wf:WebBrowser x:Name="mWebBrowser" />
   </WindowsFormsHost>
</UserControl>
发布评论

评论列表(0)

  1. 暂无评论