Windows 8 Store Apps學習(8) WebView控制項

來源:互聯網
上載者:User

介紹

重新想象 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));        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.