项目环境:vs2008 
我要实现的功能:
通过webBrowser将一个日历的html加载进来,点击日历中的日期后取得该值并在界面中显示,下面是从网上找到的测试代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Security.Permissions;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            toJS tojs = new toJS();
            webBrowser1.ObjectForScripting = tojs;
            webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/date.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {   
        }
    }    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class toJS
    {
        public void msg(string str)//JS中调用该方法回传数据
        {
            MessageBox.Show(str);
        }
    }}
因为与JS交互的方法写在类toJS中,所以现在点击某个日期后,返回的值不能立刻在页面上显示,如果要显示还需要另外点个获取按钮来获取值,请各位帮忙看看有啥好方法没
如果用下面的方法写则会报错:
A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class 'WpfApplication1.Window1'. However since this class does not have an explicit default interface and derives from non COM visible class 'System.Windows.Window', the QueryInterface call will fail. This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Security.Permissions;
namespace WpfApplication1
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //toJS tojs = new toJS();
            webBrowser1.ObjectForScripting = this;
            webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/date.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
        }
        public class toJS
        {
            public void msg(string str)
            {
                MessageBox.Show(str);
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {   
        }
    }    //[System.Runtime.InteropServices.ComVisibleAttribute(true)]
    //public class toJS
    //{
    //    public void msg(string str)
    //    {
    //        MessageBox.Show(str);
    //    }
    //}}在html中的测试方法如下:
function ReturnV()
{ window.external.msg("1111222");
}麻烦给为给个解决的办法,谢谢了,在线等!!!

解决方案 »

  1.   

    Try...[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Window1 : Window
    {
    }
      

  2.   

    没用过,不过看上去问题很简单。基于你在网上找到的代码,在toJs里申明一个EventHandler, 在msg里触发这个EventHandler并且把msg的值传给他。在window1里定义一个EventHandler即可。
    或者你就把window1传给toJs,在msg()里调用parentWindow.OnMsg()....