C# InvokeScript gives error 80020006

早过忘川 提交于 2019-12-10 23:37:24

问题


I have the piece of code as follows.

webBrowser.IsScriptEnabled = true;
webBrowser.InvokeScript("eval", "alert('hey')");

It gives An unknown error has occurred. Error: 80020006. Could you guide how to rectify this error.


回答1:


There is no built-in browser window.alert in Windows Phone, but you can bind one as follows to call WebBrowser.ScriptNotify

//inside the page
window.alert = function (__msg) { window.external.notify(' + __msg + '); };


// in your C# code
this.webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(Browser_ScriptNotify);
void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
     MessageBox.Show(e.Value);
}

//later 
this.CordovaView.Browser.IsScriptEnabled = true;
this.CordovaView.Browser.InvokeScript("alert", "ok");

On Cordova, there is also a Notification Plugin that you can plug by

window.alert = navigator.notification.alert;

Just be sure to enable Notification Plugin in config.xml

  <feature name="Notification">
      <param name="wp-package" value="Notification"/>
  </feature>



回答2:


It is caused by race condition. What we need to do is

this.CordovaView.Browser.LoadCompleted += Browser_LoadCompleted;

then

  void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        this.CordovaView.Browser.IsScriptEnabled = true;
        this.CordovaView.CordovaBrowser.IsScriptEnabled = true;
        this.CordovaView.Browser.InvokeScript("setPushToken", push_uri);
    }


来源:https://stackoverflow.com/questions/17466309/c-sharp-invokescript-gives-error-80020006

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!