從名字上就看出來,這個傢伙就是開啟瀏覽並瀏覽到指定頁面。
它有兩個用途完全一樣的屬性:Uri屬性是System.Uri類型,這是新寫進的屬性;
URL是字串類型,但如果使用該屬性,會發出警告“已淘汰”,所以建議使用前者。
下面這個例子,點擊按鈕後都是開啟WEB瀏覽器並定位到文字框中輸入的地址,但分別用了上面所說的兩個屬性,當程式運行後,你會發現其效果是一樣的。
[html] view plaincopyprint?
- <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>
[csharp] view plaincopyprint?
- 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();
- }
- }
- }