C# WebBrowser InvokeScript 给出错误

标签 c# javascript windows-phone-7 cordova invokescript

我正在开发 Windows Phone 7.5 PhoneGap 应用程序。我需要从 C# 后面的代码调用 JavaScript 函数。我的 MainPage.xaml.cs 如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Resources;



namespace MFMA
{
    public partial class MainPage : PhoneApplicationPage
    {
        WebBrowser webBrowser = new WebBrowser();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.CordovaView.Loaded += CordovaView_Loaded;
        }

        private void CordovaView_Loaded(object sender, RoutedEventArgs e)
        {
            this.CordovaView.Loaded -= CordovaView_Loaded;
            // first time load will have an animation
            Storyboard _storyBoard = new Storyboard();
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = 0,
                Duration = TimeSpan.FromSeconds(0.6),
                To = 90
            };
            Storyboard.SetTarget(animation, SplashProjector);
            Storyboard.SetTargetProperty(animation, new PropertyPath("RotationY"));
            _storyBoard.Children.Add(animation);
            _storyBoard.Begin();
            _storyBoard.Completed += Splash_Completed;
        }

        void Splash_Completed(object sender, EventArgs e)
        {
            (sender as Storyboard).Completed -= Splash_Completed;
            LayoutRoot.Children.Remove(SplashImage);
        }

        private void OnClearCookies(object sender, EventArgs e)
        {
            this.webBrowser.IsScriptEnabled = true;
            this.webBrowser.InvokeScript("clearCookies");




        }
    }
}

以下是index.html。

    <!DOCTYPE html> 
<html> 
    <head> 

        <meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1,initial-scale=1">
        <title>MFMA</title> 
        <link rel="stylesheet" href="css/jquery.mobile-1.3.1.css" />
        <link rel="stylesheet" href="css/jquery.mobile.structure-1.3.1.css" />

        <Script type="text/javascript" src="js/jquery-2.0.0.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.3.1.js"></script>
        <script type="text/javascript" src="cordova-2.4.0.js"></script>
         <script type="text/javascript" src="js/index.js"></script>

    </head>
    <body>
        <div data-role="page" id="interfacePage" class="page-bg" style="background: rgb(64,150,194) url(Icons/background_mobile.png) no-repeat center center;
    background-position: 0% 90%;background-size:100%; -webkit-background-size: cover; -moz-background-size: cover;
    -o-background-size: cover; background-size: cover;background-attachment:fixed">
            <div data-role="content" >
            </div>
        </div>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript">
            app.initialize();
            document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {

                console.log('start!!!!!!!!!!!!!!!!!!!!!!!!!!');
                var url;
                if (typeof (window.localStorage) == 'undefined') {
                    console.log('local storage not defined');
                }
                if (typeof (window.localStorage) != 'undefined') {
                    console.log('getting item');
                    url = window.localStorage.getItem("url");
                    console.log('saved URL is ' + url);
                }
                //var url = getCookie("url");
                if (url != null && url != "") {
                    window.location.href = 'http://' + url;
                } else {
                    window.location.href = 'index1.html';
                }
            }

            function clearCookies() {
                //console.log("success!!!!!!!!!!!!!!!");
                window.localStorage.removeItem('url'); 
            }

        </script>
    </body>
</html>

我收到“发生未知错误。错误:80020006。”我可以知道如何解决这个问题。我已经浏览过论坛和 Stack Overflow。我找不到解决方案。

最佳答案

有时候,你必须使用像这样的空参数

this.webBrowser.InvokeScript("clearCookies", new string[]{});

也可以直接调用

this.webBrowser.InvokeScript("eval", 
 new string[] { "window.localStorage.removeItem('url'); " });

关于C# WebBrowser InvokeScript 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335447/

相关文章:

C# 优化新对象的创建(太多,丑陋的代码)

javascript - 如何在asp mvc应用程序中构建ajax-chartjs折线图?

javascript - PayPal Express Checkout,提供 input_fields 时出错

javascript - jQuery/CSS - 将 slider 更改为超过 100% 但留在 div 内

javascript - 有关 famo.us 中的 View 的问题

windows-phone-7 - Windows Phone 7 中的本地数据库示例?

c# - Db4o StartsWith 并忽略大小写

c# - ServiceStack - 消息队列服务(验证和过滤)

windows-phone-7 - 应用栏图标文字长度

c# - 如何在 Windows Phone 7Thi 中从 Web 服务检索多个图像