Windows Phone开发(25):启动器与选择器之WebBrowserTask
从名字上就看出来,这个家伙就是打开浏览并浏览到指定页面。
它有两个用途完全一样的属性:Uri属性是System.Uri类型,这是新写进的属性;
URL是字符串类型,但如果使用该属性,会发出警告“已过时”,所以建议使用前者。
下面这个例子,点击按钮后都是打开WEB浏览器并定位到文本框中输入的地址,但分别用了上面所说的两个属性,当程序运行后,你会发现其效果是一样的。
<phone:PhoneApplicationPage x:Class="WebTask.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="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" 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浏览器" Margin="9,-7,0,0" FontSize="50"/> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Height="40" HorizontalAlignment="Left" Margin="38,57,0,0" Name="textBlock1" Text="请输入URL:" VerticalAlignment="Top" Width="286" FontSize="28"/> <TextBox Height="72" HorizontalAlignment="Left" Margin="12,122,0,0" Name="txtUrl" VerticalAlignment="Top" Width="394" /> <Button Content="通过Uri对象设置并启动" Height="79" HorizontalAlignment="Left" Margin="12,222,0,0" Name="button1" VerticalAlignment="Top" Width="394" Click="button1_Click" /> <Button Content="通过字符串设置并启动" Height="79" HorizontalAlignment="Left" Margin="9,323,0,0" Name="button2" VerticalAlignment="Top" Width="394" Click="button2_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 Microsoft.Phone.Tasks; namespace WebTask { public partial class MainPage : PhoneApplicationPage { // 构造函数 public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { // 通过Uri对象设置 WebBrowserTask wt = new WebBrowserTask(); wt.Uri = new Uri(txtUrl.Text, UriKind.Absolute); wt.Show(); } private void button2_Click(object sender, RoutedEventArgs e) { // 通过字符串设置 WebBrowserTask wt = new WebBrowserTask(); wt.URL = txtUrl.Text; wt.Show(); } } }