Windows Phone 8.1 webview打开手机应用程序

时间:2015-01-04 16:53:57

标签: webview windows-phone-8.1 windows-8.1

在我的应用程序中,我有一个加载WebView的页面。 有时我会收到导致打开另一个应用程序的Url,例如Phone \ Sms \ Store \ etc.我试图找到一种方法来检测我的Page因为这样的Url而移动到后台的情况。

有没有人有想法? THX

1 个答案:

答案 0 :(得分:0)

您有4个选项可以尝试找出WebView的位置或内容:

查看文档似乎最好的方法是NavigationStarting 事件并将WebViewNavigationStartingEventArgs.Cancel property设置为true

<WebView NavigationStarting="webView1_NavigationStarting" />

C#

我们可以使用Dictionary<string, bool>来存储和检查我们要访问的网页是否是有效的网页

private Dictionary<string, bool> knownWebSites;
void webView1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
    string url = "";
    try { 
        url = args.Uri.ToString();
        if(knownWebSites.ContainsKey[url])
        {
             if(knownWebSites[url] == flase)
             {
                 args.Cancel = true;
             }
             // contine this is a known webpage without issues
        }
        else
        {
             args.Cancel = true;
             CheckHttpWebpage(url); // first inspect the page
        }
    }
    finally
    {
       // Do stuff
    }
}

async void CheckHttpWebpage(string url)
{
     HttpClientResponse response = await HttpGet(url); // this is a custom http get function
     if( IsValidResponse(response))
     {
         knownWebSites.Add(url, false); // this page has issues
     }
     else
     {
         knownWebSites.Add(url, true); // this page is good
         webview.Navigate(new Uri(url));
     }
}

bool IsValidResponse(HttpClientResponse response)
{
   // In here you can examine 
   // 1. response content to make sure it does not return phone numbers, and you have it, so you can do something pretty with it now :-)
   // 2. url to make sure it isn't a redirect
   // 3. lots of other stuff to ensure that the navigating to webpage is valid
}

如果网页需要授权,建议使用HttpWebResponse