介紹
重新想象 Windows 8 Store Apps 之 WebView
示範 WebView 的基本應用
示範 WebView 如何與 JavaScript 互動
通過 Share Contract 分享 WebView 中的內容
如何全屏 WebView
樣本
1、WebView 的基本應用
WebView/Demo.xaml
<Page x:Class="XamlDemo.Controls.WebView.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls.WebView" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <Button Name="btnNavigateUrl" Content="導航到指定的 url" Click="btnNavigateUrl_Click_1" /> <Button Name="btnShowHtml" Content="解析指定的 html 字串" Click="btnShowHtml_Click_1" Margin="0 10 0 0" /> <Button Name="btnFullScreen" Content="示範當 WebView 全屏時如何操作當前頁(WebView 會遮擋所有元素)" Click="btnFullScreen_Click_1" Margin="0 10 0 0" /> <WebView Name="webView" Width="400" Height="300" HorizontalAlignment="Left" Margin="0 10 0 0" /> </StackPanel> </Grid></Page>
WebView/Demo.xaml.cs
/* * WebView - 內嵌瀏覽器 * Source - 導航到指定的 url * Navigate() - 導航到指定的 url * NavigateToString() - 解析指定的 html 字串 * LoadCompleted - 在 DOM 載入完成後所觸發的事件 * NavigationFailed - 導航發生錯誤時觸發的事件(事件參數:WebViewNavigationFailedEventArgs) * * WebViewNavigationFailedEventArgs * Uri - 嘗試導航到的 Uri * WebErrorStatus - 錯誤狀態(Windows.Web.WebErrorStatus 枚舉) */ using System;using Windows.UI.Popups;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls.WebView{ public sealed partial class Demo : Page { public Demo() { this.InitializeComponent(); } private void btnNavigateUrl_Click_1(object sender, RoutedEventArgs e) { // 導航到指定的 url webView.Navigate(new Uri("http://webabcd.cnblogs.com/", UriKind.Absolute)); // webView.Source = new Uri("http://webabcd.cnblogs.com/", UriKind.Absolute); // 導航失敗時 webView.NavigationFailed += webView_NavigationFailed; } async void webView_NavigationFailed(object sender, WebViewNavigationFailedEventArgs e) { await new MessageDialog(e.WebErrorStatus.ToString()).ShowAsync(); } private void btnShowHtml_Click_1(object sender, RoutedEventArgs e) { // 解析指定的 html 字串 webView.NavigateToString("<html><body>I am webabcd</body></html>"); } private void btnFullScreen_Click_1(object sender, RoutedEventArgs e) { var root = Window.Current.Content as Windows.UI.Xaml.Controls.Frame; root.Navigate(typeof(XamlDemo.Controls.WebView.FullScreen)); } }}