Web控件

Web控件,即Web Browser控件,是Windows Phone 7提供的浏览器控件,使用Web控件用户可以显示互联网网页、静态网页内容以及动态生成的Web内容。显示互联网网页,典型的应用是在一个独立的页面中使用Web控件,显示自定义的Web内容,并且控制Web控件之外的页面外观,从而获取与内置浏览器不同的用户体验。

  • 显示静态信息,典型的应用时使用独立存储将Web页面存储在本地,然后使用Web控件读取并显示。
  • 显示动态信息,典型的应用就是程序员使用代码动态生成html内容,并使用Web控件展示。

WebBrowser的一个重要属性就是Source,它的值数Uri类型,它用来获取或设置要在 WebBrowser 控件中显示的 HTML 内容的 URI 源。这个当然可以在后台代码中改变,另外,它的两个重要方法是

  • Navigate, 加载位于指定 URI 中的 HTML 内容。
  • NavigateToString,显示指定的 HTML 内容。
  • WebBrowser.InvokeScript,执行在当前加载的 HTML 中定义的指定脚本。

做一个简单的例子来说明一下显示网页和动态加载网页

<phone:PhoneApplicationPage 
x:Class="Controls.WebBrowser"
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"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="Portrait" Orientation="Portrait"
mc:Ignorable
="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible
="True">

<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Web Browser" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:WebBrowser HorizontalAlignment="Left" Margin="0,136,0,0"
Name
="myWebBrowser" VerticalAlignment="Top" Height="465"
Width
="450" Source="http://www.baidu.com" />
<Button Content="显示Mine" Height="72" HorizontalAlignment="Left"
Margin
="269,40,0,0" Name="myButton" VerticalAlignment="Top" Width="160"
Click
="myButton_Click"/>
</Grid>
</Grid>

</phone:PhoneApplicationPage>

后台代码(实现切换):

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 Microsoft.Phone.Controls;
using System.Text;

namespace Controls
{
public partial class WebBrowser : PhoneApplicationPage
{
public WebBrowser()
{
InitializeComponent();
}

private void myButton_Click(object sender, RoutedEventArgs e)
{
if (this.myButton.Content.ToString() == "显示Mine")
{
this.myButton.Content = "返回";
StringBuilder html = new StringBuilder();
html.Append("<html><head><title>www</title></head><body>test</body></html>");
this.myWebBrowser.NavigateToString(html.ToString ());
}
else if(this.myButton.Content.ToString ()=="返回")
{
this.myButton.Content = "显示Mine";
this.myWebBrowser.Source =new Uri("http://www.baidu.com",UriKind.Absolute);
}
}
}
}

当然,点击按钮可以实现切换。如下:

但是有一点,<title>标签中不能使用汉字,使用了之后会显示不出来页面的内容,不知道是什么原因,或者是因为编码的问题,知道的朋友补充一下吧。

特点:
从网络加载的内容具有跨域调用的限制,但是从本地独立存储中加载的内容或是使用NavigateToString()方法加载的内容不受跨域调用的限制;可以直接访问独立存储;可以显示HTML,同时可以使用页面中的链接和导航功能;不包含HTMLBrush控件,不能使用ActiveX控件;应用程序和Web控件已经共享Cookies。

其他:
Web控件不提供链接地址的查看,也不提供访问安全页面时的安全锁图标。
因为Web控件没有提供全部的网络安全特性,因此不应该使用Web控件来实现通用的浏览器应用程序。
Web控件所在的应用程序不能和手机自带的Internet Explorer Mobile共享Cookie。
默认状态下脚本在Web控件中是被禁用的,但是可以通过设置属性IsScriptEnabled来打开。

posted on 2012-04-08 17:07  WaitingSky  阅读(1774)  评论(3编辑  收藏  举报