C# 指定的名称(外部)是保留的。WPF选项卡项上的WebControl异常

C# 指定的名称(外部)是保留的。WPF选项卡项上的WebControl异常,c#,wpf,awesomium,tabitem,C#,Wpf,Awesomium,Tabitem,我正在尝试使用Awesomium 1.7.4.2创建一个简单的WPF选项卡式WebControl视图。我的想法是,我有一个主ClosableTab(它扩展了TabItem),其中包含一个网络控件(比方说它是HomeTab),然后加载一个特定的url。从HomeTab用户可以打开其他选项卡,即每个新网页在包含WebControl的新选项卡项处打开。每个WebControl都有以下处理程序: webTabControl.CertificateError += webContr

我正在尝试使用Awesomium 1.7.4.2创建一个简单的WPF选项卡式WebControl视图。我的想法是,我有一个主
ClosableTab
(它扩展了
TabItem
),其中包含一个网络控件(比方说它是
HomeTab
),然后加载一个特定的url。从
HomeTab
用户可以打开其他选项卡,即每个新网页在包含
WebControl
的新选项卡项处打开。每个WebControl都有以下处理程序:

            webTabControl.CertificateError += webControl_CertificateError;
            webTabControl.NativeViewInitialized += webControl_NativeViewInitialized;
            webTabControl.ShowContextMenu += webControl_ShowContextMenu;
            webTabControl.LoadingFrameComplete += webControl_LoadingFrameComplete;
            webTabControl.LoadingFrameFailed += webControl_LoadingFrameFailed;
  • webControl\u nativeviewsinitialized
    上,我创建了一个名为
    DotApi
    的全局javascript对象,然后在其上绑定一些javascript方法,如
    openTab

  • 有一个名为
    JSHandler
    的方法,用于处理
    DotApi
    方法的结果

  • 当从
    HomeTab
    调用
    DotApi.openTab
    时,将创建一个新选项卡,添加到
    webTabControl
    tab\u control
    )中,然后显示(如果用户用鼠标左键单击)

  • 在加载的
    tabItem\u上创建一个新的
    webControl
    ,该控件绑定上述
    webControl
    处理程序,并作为内容添加到
    tabItem

  • 如果用户用鼠标中键单击,则会调用
    webControl.ApplyTemplate()
    (由于鼠标中键单击,因此会在后台打开新选项卡,即新的
    TabItem
    不会聚焦)

这似乎很直截了当。这几乎每次都很有效。问题是百分之一的错误会出现:

The specified name (DotApi), is reserved. It either represents an HTML DOM object, or an object variable already available in the page. Use ExecuteJavascriptWithResult after DocumentReady, to obtain this object.
Parameter name: name
我注意到,只有出现以下错误时,才会出现上述错误:

A first chance exception of type 'System.ArgumentException' occurred in Awesomium.Windows.Controls.dll
我查看了debug.log并注意到此错误与我的错误类似,只是在指定的名称上有所不同:

The specified name (external), is reserved. It either represents an HTML DOM object, or an object variable already available in the page. Use ExecuteJavascriptWithResult after DocumentReady, to obtain this object.
Parameter name: name
我的问题是:

-为什么这种错误很少发生

-怎么能修好呢

下面是帮助您理解流程的示例代码:

网络控制\u NativeViewInitialized

  private void webControl_NativeViewInitialized(object sender, WebViewEventArgs e)
        {
            WebControl wbControl = sender as WebControl;
            try
            {
                JSObject jsobject = wbControl.CreateGlobalJavascriptObject("DotApi");
                // openTab will be invoked each time a new page opens in new tab
                jsobject.Bind("openTab", false, JSHandler);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("NativeViewInitialized exception: " + ex.Message);
            }
        }
        private void theTabItem_Loaded(object sender, RoutedEventArgs e, JSValue[] arguments)
        {
            ClosableTab currentTab = sender as ClosableTab;
            currentTab.Loaded -= (s_, e_) => theTabItem_Loaded(currentTab, e_, arguments);

            WebControl webTabControl = new WebControl();

            webTabControl.Source = new Uri((string)arguments[0]);
            webTabControl.CertificateError += webControl_CertificateError;
            webTabControl.NativeViewInitialized += webControl_NativeViewInitialized;
            webTabControl.ShowContextMenu += webControl_ShowContextMenu;
            webTabControl.LoadingFrameComplete += webControl_LoadingFrameComplete;
            webTabControl.LoadingFrameFailed += webControl_LoadingFrameFailed;
            currentTab.Content = webTabControl;

            webTabControl.Dispatcher.BeginInvoke(new Action(() =>
            {
                    // Load the webcontrol in the backgound
                    if(!currentTab.IsSelected){
                        webTabControl.ApplyTemplate();
                    }
                }));
        }
JSHandler

        private void JSHandler(object sender, JavascriptMethodEventArgs args)
        {
            try
            {
                if (!args.MustReturnValue)
                {
                     case @"openTab": // parameters: url, type(order,index,map), true/fasle(focus on new tab),arguments 
                            if (args.Arguments.Length > 0)
                            {
                                Application.Current.Dispatcher.BeginInvoke(
                                        new OpenNewTabDelegate(OpenNewTab), args.Arguments);
                            }
                            break;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("JSHandler Exception: " + e.Message);
            }
        }
OpenNewTab

        private void OpenNewTab(JSValue[] arguments)
        {
            try
            {
                ClosableTab theTabItem = new ClosableTab(this);
                theTabItem.Title = "Loading";

                theTabItem.Loaded += (s_, e_) => theTabItem_Loaded(theTabItem, e_, arguments);

                tab_control.Items.Add(theTabItem);
                if (!(bool)arguments[2])  // whether it focus on new window or not
                {
                    theTabItem.Focus();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("OpenNewTab exception e: " + e.Message);
            }            
        }
加载的数据库项目

  private void webControl_NativeViewInitialized(object sender, WebViewEventArgs e)
        {
            WebControl wbControl = sender as WebControl;
            try
            {
                JSObject jsobject = wbControl.CreateGlobalJavascriptObject("DotApi");
                // openTab will be invoked each time a new page opens in new tab
                jsobject.Bind("openTab", false, JSHandler);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("NativeViewInitialized exception: " + ex.Message);
            }
        }
        private void theTabItem_Loaded(object sender, RoutedEventArgs e, JSValue[] arguments)
        {
            ClosableTab currentTab = sender as ClosableTab;
            currentTab.Loaded -= (s_, e_) => theTabItem_Loaded(currentTab, e_, arguments);

            WebControl webTabControl = new WebControl();

            webTabControl.Source = new Uri((string)arguments[0]);
            webTabControl.CertificateError += webControl_CertificateError;
            webTabControl.NativeViewInitialized += webControl_NativeViewInitialized;
            webTabControl.ShowContextMenu += webControl_ShowContextMenu;
            webTabControl.LoadingFrameComplete += webControl_LoadingFrameComplete;
            webTabControl.LoadingFrameFailed += webControl_LoadingFrameFailed;
            currentTab.Content = webTabControl;

            webTabControl.Dispatcher.BeginInvoke(new Action(() =>
            {
                    // Load the webcontrol in the backgound
                    if(!currentTab.IsSelected){
                        webTabControl.ApplyTemplate();
                    }
                }));
        }

调试之后,我发现这是来自awesomium的timedOut异常。为了处理它,每次创建webControl时,我都设置它的属性:
SynchronousMessageTimeout=0
。如果不将其设置为0,根据当前文档,默认值将为800ms

这防止出现以下异常:

A first chance exception of type 'System.ArgumentException' occurred in Awesomium.Windows.Controls.dl

The specified name (DotApi), is reserved. It either represents an HTML DOM object, or an object variable already available in the page. Use ExecuteJavascriptWithResult after DocumentReady, to obtain this object.
Parameter name: name

因为这也会发生在我身上,很难说这是否解决了问题,但我会密切关注并让你知道。