Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(8) WebView控件

Windows 8 Store Apps学习(8) WebView控件2013-12-04 博客园 webabcd介绍

重新想象 Windows 8 Store Apps 之 WebView

演示 WebView 的基本应用

演示 WebView 如何与 JavaScript 交互

通过 Share Contract 分享 WebView 中的内容

如何全屏 WebView

示例

1、WebView 的基本应用

WebView/Demo.xaml

<Pagex: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){// 导航到指定的 urlwebView.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));}}}