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

Goldfire_001 2010-04-12 09:03:57
这个问题已经折磨我两天了。

哪位大大快来救一下:

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

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

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

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

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

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

或者有其他什么办法,也行,只要实现他们的交互就行。

...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingChina 2010-07-19
  • 打赏
  • 举报
回复
补充
debug->exceptions->managed debugging assistants中把NonComVisbleBaseClass的Thrown去掉,可以去掉NonComVisbleBaseClass错误,但是如果接着提示js错误的话,可以按上面的方法修改window1.xaml代码

呵呵

GOOD LUCK!
KingChina 2010-07-19
  • 打赏
  • 举报
回复
对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-13
  • 打赏
  • 举报
回复
额,这个我关了。然后出现一下的错误提示
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");
}
}

XamlCode

<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 Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Test</title>
<script type="text/javascript">
function JSCallWPF() {
window.external.JsCallWPF();
}
</script>
</head>
<body>
<div id="footer">
<p>CopyRight(c)2003-2008, GrapeCity inc,All rights reserved</p>
</div>
</body>
</html>

看样子好像是权限问题。这咋解决呢?
jv9 2010-04-12
  • 打赏
  • 举报
回复
你使用JavaScript调用WPF里什么方法? 试着关闭NonComVisibleBaseClass的异常抛出,看看怎样?
1. Debug->Exceptions...
2. 展开"Managed Debugging Assistants"
3. 反选NonComVisibleBaseClass
4. 确认

8,719

社区成员

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

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