Windows Phone 提供基於桌面瀏覽器的 WebBrowser 控件。Windows Phone OS 7.1 的 WebBrowser 控件基於 Internet Explorer 9,且 Windows Phone 8 的 WebBrowser 控件基於 Internet Explorer 10。所以,與在 Windows Phone 8 上運行的手機相比,使用在 Windows Phone 7.5 運行的 WebBrowser 的控件的應用,其外觀稍有不一樣。android
WebBrowser 控件能夠嵌入應用中而且使用它的緣由不少,不只限於如下緣由:web
● 顯示網絡中的 Web 內容。您能夠構建一個應用,該應用只是由指向您網站的嵌入 WebBrowser 控件組成,在該控件外面有自定義標記。有關更多信息,請參見如何使用Windows Phone的WebBrowser控件顯示網絡中的 Web 內容。shell
● 顯示靜態的 Web 內容。能夠將應用配置爲在本地獨立存儲中保存的內容,而後在嵌入的 WebBrowser 控件中查看這些內容。有關更多信息,請參見如何使用 Windows Phone 的 WebBrowser 控件顯示靜態 Web 內容。express
● 顯示動態生成的 Web 內容。能夠將 WebBrowser 控件指向應用代碼中動態構造的 HTML 內容。有關更多信息,請參見如何使用Windows Phone的WebBrowser控件顯示動態生成的 Web 內容。編程
默認狀況下,腳本在 WebBrowser 控件中處於禁用狀態。若是您想在控件中啓用腳本,請將 IsScriptEnabled 屬性設置爲 true。可以使用 InvokeScript 方法調用腳本。當 WebBrowser 控件中的 JavaScript 將字符串傳遞到託管的代碼時,將發生 ScriptNotify 事件。windows
警告:當您在 XAML 中建立 WebBrowser 控件時,您必須爲該控件的 System.Windows.FrameworkElement.Name 屬性指定值,以便 Windows Phone 功能檢測工具可以合理地檢測和賦予應用正確的功能。有關 Windows Phone 功能檢測工具的詳細信息,請參見如何肯定應用功能。瀏覽器
Windows8 中的WebView提供承載應用程序內 HTML 內容的 UI 元素。WebView 始終使用文檔模式中 Internet Explorer 10。與WP8的WebBrowser相似,WebView能夠顯示網絡中的 Web 內容、靜態的 Web 內容以及動態生成的 Web 內容。具體使用方法咱們稍後進行介紹。安全
須要注意的是,WebView 目前不支持某些 HTML5 功能,包括 AppCache、IndexedDB、對剪貼板的編程訪問或地理位置。此外,WebView 不支持 ms-appdata: scheme,可是其支持 ms-appx-web: schemecookie
若是你想實現一個Web應用(或僅僅是一個網頁)做爲你應用中的一部分,你可使用WebView來實現它。WebView是Android的View類的擴展,它容許你顯示一個網頁做爲Activity佈局的一部分。它不包含成熟的瀏覽器的一些功能,例如導航控制或輸入欄。默認狀況下,WebView顯示一個網頁。網絡
一個使用WebView的很廣泛的場合是當你想要在應用中提供須要時常更新的信息時,例如用戶協議或用戶手冊。在Android應用當中,你能建立一個Activity它包含一個WebView,而後使用它你能夠顯示網上提供的文檔。
另外一個使用WebView的場合是:假如你的應用老是須要網絡連接來獲取數據並提供給用戶,例如電子郵件。這時候,你能發如今應用中建立一個WebView並使用網頁來顯示用戶數據,比執行網絡請求,解析數據而後再渲染到Android佈局當中更加方便。你只須要根據Android設備設計網頁,而後在應用當中實現一個WebView而後載入剛剛設計的網頁。
開發使用 WebBrowser 控件的應用程序時,考慮如下有關安全的最佳作法和信息:
● 從獨立存儲加載的內容或使用 NavigateToString(String) 方法動態加載的內容的處理方式不一樣於從網絡加載的內容。從獨立存儲加載的內容或使用 NavigateToString 動態加載的內容沒有跨站點限制,但從網絡加載的內容有跨站點限制。所以,在從獨立存儲加載或使用 NavigateToString 動態加載不受信任的內容時應格外當心。
● 請注意傳遞給 InvokeScript(String) 方法的數據的敏感性。例如,隱私或位置數據不該該向不受信任的腳本代碼公開。
● 不該該使用 WebBrowser 控件建立常規用途的瀏覽器應用程序,由於此 API 並未設計爲支持此類應用程序所需的所有安全功能。
● WebBrowser 控件不提供查看 URL 或安全鎖定圖標的功能。
● 在 WebBrowser 控件中,用戶不能從 https 頁面導航到 http 頁面。這不一樣於設備瀏覽器,在設備瀏覽器中用戶能夠從 https 頁面導航到 http 頁面。
● 應用程序不能與 Internet Explorer Mobile 共享 Cookie。
● 除非在應用程序中將 IsScriptEnabled 屬性設置爲 true,不然默認狀況下在瀏覽器中禁用腳本。
● WebView 不是Control子類且沒有控件模板。顯示區域爲Width和Height。
● WebView 具備其餘 UI 區域特徵,如控件沒法呈如今 WebView 頂部。這問題的緣由是如何處理窗口區域內部,尤爲是輸入事件是如何處理以及屏幕是如何繪製的。若是您要呈現 HTML 內容而且還要將其餘用戶界面元素放置在該 HTML 內容的頂部,則應使用WebViewBrush做爲呈現區域。WebView 仍提供 HTML 源信息,您可經過元素名稱綁定和 SourceName 屬性引用該 WebView。WebViewBrush 沒有此覆蓋限制。
● 若是要顯示一個偶爾有交互的WebView(例以下拉列表或應用程序欄),能夠在必要時臨時隱藏 WebView 控件,將其替換爲一個使用WebViewBrush填充的元素。而後,當重疊的內容不存在時,能夠再次顯示原始 WebView。
WebView 不支持許多像KeyDown、KeyUp和PointerPressed的UIElement事件。此問題的常看法決方法是將WebView.InvokeScript與 eval 一塊兒使用以便使用 HTML 事件處理程序,以及使用WebView.ScriptNotify以便使用 HTML 事件處理程序的 window.external.notify 來通知應用程序。
能夠經過將 WebBrowser (WebView)控件託管在應用程序中並使用其 Source 屬性或 Navigate(Uri) 方法更改該控件的位置來實現該操做。
警告: 默認狀況下,腳本在 WebBrowser 控件中處於禁用狀態。若是您想在控件中啓用腳本,請將 IsScriptEnabled 屬性設置爲 true。
如下代碼示例顯示如何從 .xaml 文件內部更新 WebBrowser (WebView)控件的 Source 屬性:
Wp8:
<phone:WebBrowser Source="http://www.bing.com" />
Win8:
<WebView Source=" http://www.bing.com "/>
或者,若是在 .xaml 文件中爲 WebBrowser (WebView)控件指定了一個名稱,則能夠從該代碼隱藏文件內部更新 Source 屬性。如下代碼示例顯示如何更新 WebBrowser 控件(在 .xaml 文件中已命名爲 webBrowser1(webView1))的 Source 屬性:
Wp8:
webBrowser1.Source = new Uri("http://www.bing.com", UriKind.Absolute);
Win8:
webView1.Source = new Uri("http://www.bing.com", UriKind.Absolute);
或者,也可使用 WebBrowser(WebView)類的 Navigate(Uri) 方法來實現該目標:
Wp8:
webBrowser1.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));
Win8:
webView.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));
若是您選擇調用方法而不是設置屬性,那麼請記住,若是WebBrowser控件尚不在可視化樹中,則會引起 InvalidOperationException。爲了不這個問題,您能夠向 Loaded 事件附加一個處理程序,以確保在調用該方法以前此控件位於可視化樹中。
Wp8:
webBrowser1.Loaded += (object sender, RoutedEventArgs e) => { webBrowser1.Navigate(new Uri("http://www.bing.com", UriKind.Absolute)); };
示例
下面的示例演示如何建立 Web 請求並向該請求添加 Cookie。此示例還演示如何從 Web 響應提取 Cookie,如何將 Cookie 寫入獨立存儲中的文件以及從獨立存儲中進行讀取。運行此示例時,將在 TextBlock 控件中顯示 System.Net.Cookie 值。
C#
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 System.Net.Browser; using System.IO; using System.Text; using System.IO.IsolatedStorage; namespace CookiesEx { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { InitializeWebRequestClientStackForURI(); ReadFromIsolatedStorage(); } private void InitializeWebRequestClientStackForURI() { // Create a HttpWebRequest. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx")); //Create the cookie container and add a cookie. request.CookieContainer = new CookieContainer(); // This example shows manually adding a cookie, but you would most // likely read the cookies from isolated storage. request.CookieContainer.Add(new Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx"), new Cookie("id", "1234")); // Send the request. request.BeginGetResponse(new AsyncCallback(ReadCallback), request); } // Get the response and write cookies to isolated storage. private void ReadCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asynchronousResult); using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies", FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(isfs)) { foreach (Cookie cookieValue in response.Cookies) { sw.WriteLine("Cookie: " + cookieValue.ToString()); } sw.Close(); } } } } private void ReadFromIsolatedStorage() { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies", FileMode.OpenOrCreate)) { using (StreamReader sr = new StreamReader(isfs)) { tb1.Text = sr.ReadToEnd(); sr.Close(); } } } } } }
XAML
<phone:PhoneApplicationPage x:Class="CookiesEx.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"> <StackPanel x:Name="LayoutRoot" Width="390"> <Button Width="372" Height="173" Content="Click to send request" HorizontalAlignment="Left" x:Name="button1" Click="button1_Click" Margin="5"/> <TextBlock TextWrapping="Wrap" Foreground="{StaticResource PhoneForegroundBrush}" Height="267" Name="tb1" Width="382" FontSize="22" /> </StackPanel> </phone:PhoneApplicationPage>
WinRT對WebView的功能作了不少的限制,因此自己webview控件是獲取不到cookie咱們能夠用HttpWebRequest的CookieContainer來完成cookie的設置。
在android裏面,爲使HttpClient中保存下來的Cookie設置到WebView組件裏,讓WebView也保持住Cookie狀態,咱們須要用到CookieSyncManager和CookieManager類。在webView調用webView.loadUrl方法以前作如下設置便可給WebView設置Cookie
{ InitializeComponent(); SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape; webBrowser1.Loaded += WebBrowser_OnLoaded; } private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e) { SaveFilesToIsoStore(); webBrowser1.Navigate(new Uri("readme.htm", UriKind.Relative)); }