這是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