求救!!!!!!!!!!

Goldfire_001 2010-04-13 09:55:30
这个问题已经折磨我两天了。

哪位大大快来救一下:

问题:
WPF application里面嵌了一个Winform的Webbrowser,然后加载一个Html。

这个Html里面有一些JavaScript方法。

我只是想让WPF和JavaScript交互,你调用我的方法,我调用你的方法。仅此而已。

现在问题是。WPF调用JavaScript的方法,没有问题。
但是反过来,JavaScript调用WPF里面方法的时候,就不行了,抛出一个诡异的NonComVisibleBaseClass的异常

注:1。该打的Attribute我都打了,该写的东西我也写了。就是不行。
2。同样的Code,在Winform里面是没有问题的。

不知道各位大大有没有搞过类似方面的东西。给小弟点希望吧。

或者有其他什么办法,也行,只要实现他们的交互就行。
...全文
192 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingChina 2010-07-19
  • 打赏
  • 举报
回复
debug->exceptions->managed debugging assistants中把NonComVisbleBaseClass的Thrown去掉
如果不行的话,需要对window1.xaml代码做修改:

对js要调用的c#代码封装成一个类,然后在Window_Loaded中申明。具体代码
window1.xaml.cs
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window_Loaded);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PageFunctionClass ds = new PageFunctionClass();
this.WebBrowser1.ObjectForScripting = ds;
this.WebBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/HTMLPage1.htm", UriKind.RelativeOrAbsolute));
WebBrowser1.LoadCompleted += BrowserOnLoadCompleted;
}
}
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class PageFunctionClass
{
public void ShowCallData(String str)
{
MessageBox.Show("这是WPF的窗口,从页面得到数据:" + str );
}
}
}

js代码:
window.external.ShowCallData(val);

good luck!
Goldfire_001 2010-04-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 llszn 的回复:]
好像很简单,右击工程选属性。有个选项,应用程序->程序集信息->使程序集com可见。勾上就行。
[/Quote]
...大侠,那个勾勾默认就是勾上滴。
llszn 2010-04-16
  • 打赏
  • 举报
回复
好像很简单,右击工程选属性。有个选项,应用程序->程序集信息->使程序集com可见。勾上就行。
Goldfire_001 2010-04-16
  • 打赏
  • 举报
回复
顶一下,希望大大们关注下。
Goldfire_001 2010-04-14
  • 打赏
  • 举报
回复
还请诸位高手指点一下。。是不是我的思路本身有问题呢?
Goldfire_001 2010-04-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 timdavid 的回复:]
按你的做法,你在WPF里添加了一WinForm,然后WinForm里显示HTML。在HTML里面用Javascript。
那么你让JavaScript能不调用WinForm的方法,之后再让WPF和WinForm通讯。这样可能是一条思路。
我没做过,希望你给你点提示,思路上也好。
Up!
[/Quote]
我先写一个Winform的UserControl。把WebBrowser塞里面,并写好Js调用的方法,然后把这个UserControl加到WindowsFormHost里面去?

这也许是个办法,但是太不直接了,而且多了一个Assembly。。。

我感觉应该是有解决的方法的,只是没有找到,因为WPF可以调用JavaScript的,但是JavaScript不能反调用。
这个有点有悖于理论么。

哎,头大啊。已然没有解决。
TimDavid 2010-04-13
  • 打赏
  • 举报
回复
帮手顶下,
觉得,JScript我觉得能操作HTML的DOM对象
用它来调用WPF中的方法,你去看看,WPF有没像Silverlight中的注册给Javascript的特性。
有可能就没问题。
Goldfire_001 2010-04-13
  • 打赏
  • 举报
回复
关闭NonComVisibleBaseClass异常后出现如下Internet Explorer Script Error
Error:Unexpected call to method or property access:
看样子好像是权限问题。这咋解决呢?

大体流程上面是这样。

[PermissionSet(SecurityAction.Demand,Name="FullTrust")]
[ComVisibleAttribute(true)]
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.webBrowser.ObjectForScripting = this;
this.webBrowser.Navigate(new Uri(Environment.CurrentDirectory + @"\default.html"));
}

private void Button_Click(object sender, RoutedEventArgs e)
{
HtmlDocument document = this.webBrowser.Document;
document.InvokeScript("JSCallWPF", null);
}

public void JsCallWPF()
{
System.Windows.MessageBox.Show("JS CALL WPF");
}
}



<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:host="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winControl="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<host:WindowsFormsHost Grid.Row="0">
<winControl:WebBrowser x:Name="webBrowser"/>
</host:WindowsFormsHost>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Button Content="Js call WPF" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>



<html>
<head>
<title>Test</title>
<script type="text/javascript">
function JSCallWPF() {
window.external.JsCallWPF();
}
</script>
</head>
<body>
<div>
<p>Please Help Me</p>
</div>
</body>
</html>
TimDavid 2010-04-13
  • 打赏
  • 举报
回复
按你的做法,你在WPF里添加了一WinForm,然后WinForm里显示HTML。在HTML里面用Javascript。
那么你让JavaScript能不调用WinForm的方法,之后再让WPF和WinForm通讯。这样可能是一条思路。
我没做过,希望你给你点提示,思路上也好。
Up!
Goldfire_001 2010-04-13
  • 打赏
  • 举报
回复
我现在开始感觉是因为WindowsFormsHost的问题了。

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧