WebBrowser完成加载Windows Phone后返回一个值

时间:2015-03-11 15:48:42

标签: windows-phone-8

我正在尝试使用js从WebBrowser检索数据。 我必须等待我的webView加载然后执行我的js代码然后我的方法返回值。 这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Threading;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows;


namespace MobilAirSdk.Analytics.utils
{
    public class UserAgentHelper
    {
        static private Dictionary<string, string> uaDict;
        static readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
        static WebBrowser browser;

        static string displayValue = null;


        public static Dictionary<string, string> GetUserAgent()
        {
            if (uaDict == null)
            {
                uaDict = new Dictionary<string, string>();

                Deployment.Current.Dispatcher.BeginInvoke( new Action( () =>
                {

                browser = new WebBrowser ();
                browser.IsScriptEnabled = true;
                browser.LoadCompleted += browser_LoadCompleted;

                browser.NavigateToString(@"<!DOCTYPE html><html><body onload=""window.external.notify(navigator.userAgent);""><script type='text/javascript'>  function fingerprint_display() {  'use strict';  var strSep, strPair, strOnError, strScreen, strDisplay, strOut;  strSep = '|';  strPair = '=';  strOnError = 'Error';  strScreen = null;  strDisplay = null;  strOut = null;  try {  strScreen = window.screen;  if (strScreen) {  strDisplay = strScreen.colorDepth + strSep + strScreen.width + strSep + strScreen.height + strSep + strScreen.availWidth + strSep + strScreen.availHeight; }  strOut = strDisplay;  return strOut;  } catch (err) {  return strOnError; } }</script></body></html>");


                  }));

                thread1Step.WaitOne();

            }
            return uaDict;
        }

        static void browser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            displayValue = (string)browser.InvokeScript("fingerprint_display");
            uaDict.Add("display", displayValue);
            thread1Step.Set();
        }    
    }
}

问题是我的browser.Navigating从未被调用,应用程序仍然被阻止。 你知道吗? 谢谢

1 个答案:

答案 0 :(得分:0)

您好,感兴趣的是解决方案:

public class UserAgentHelper
{
    static private Dictionary<string, string> uaDict;
    static readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
    static WebBrowser browser;

    static string displayValue = null;


    public async static Task<Dictionary<string, string>> GetUserAgent()
    {
        TaskCompletionSource<Dictionary<string, string>> tcs = new TaskCompletionSource<Dictionary<string, string>>();

        if (uaDict == null)
        {
            uaDict = new Dictionary<string, string>();


            LoadCompletedEventHandler loadCompleted = null;
            loadCompleted = (_, __) =>
            {
                browser.LoadCompleted -= loadCompleted;
                displayValue = (string)browser.InvokeScript("fingerprint_display");
                uaDict.Add("display", displayValue);
                tcs.TrySetResult(uaDict);
            };

            Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
            {

                browser = new WebBrowser();
                browser.IsScriptEnabled = true;
                browser.LoadCompleted += loadCompleted;

                browser.NavigateToString(@"<!DOCTYPE html><html><body onload=""window.external.notify(navigator.userAgent);""><script type='text/javascript'>  function fingerprint_display() {  'use strict';  var strSep, strPair, strOnError, strScreen, strDisplay, strOut;  strSep = '|';  strPair = '=';  strOnError = 'Error';  strScreen = null;  strDisplay = null;  strOut = null;  try {  strScreen = window.screen;  if (strScreen) {  strDisplay = strScreen.colorDepth + strSep + strScreen.width + strSep + strScreen.height + strSep + strScreen.availWidth + strSep + strScreen.availHeight; }  strOut = strDisplay;  return strOut;  } catch (err) {  return strOnError; } }</script></body></html>");

            }));

            return await tcs.Task;
        }
        else return uaDict;

    }
}