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



我正在尝试使用 js 从 Web 浏览器中检索数据。我必须等待我的 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();
        }    
    }
}

问题是我的浏览器。导航永远不会被调用,并且应用程序仍然被阻止。你知道吗?谢谢

对于那些感兴趣的人,您好是解决方案:

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;
    }
}

最新更新