这是WPF的解决方法。
MainWindow.xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=system.windows.forms"
>
<Grid>
<WebBrowser x:Name="webBrowser" SnapsToDevicePixels="True" >
</WebBrowser>
</Grid>
</Window>
MainWindow.xaml.cs:
public void Run(Uri uri)
{
m_gateway = new HostGateway
{
MyComponent = SomeNativeLib.SomeNativeComponent
};
webBrowser.ObjectForScripting = m_gateway;
webBrowser.Navigate("about:blank");
webBrowser.Navigate(uri);
}
[ComVisible(true)]
public class HostGateway
{
public SomeNativeLib.SomeNativeComponent MyComponent {get;set;}
}
而且我们需要的本地库添加为引用:
<Reference Include="SomeNativeLib, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\..\References\SomeNativeLib.dll</HintPath>
</Reference>
然后在我们的客户js代码,我们必须通过window.external访问HostGateway实例:
window.external.MyComponent.foo();
你有没有解决过这个问题?我有完全相同的问题!问候。 – 2011-07-15 17:24:10
不,我没找到解决问题的办法。但我使用了一种解决方法:我在.net主机(WebBrowser所在的位置)中创建了ActiveX,并使其可用于WebBrowser内的JS代码。 – Shrike 2011-07-22 21:53:58
您能否为您的解决方案提供详细信息,包括代码示例?我有类似的问题!问候。 – 2015-03-05 10:05:31