Windows Phone開發(25):啓動器與選擇器之WebBrowserTask .

從名字上就看出來,這個傢伙就是打開瀏覽並瀏覽到指定頁面。

它有兩個用途完全一樣的屬性:Uri屬性是System.Uri類型,這是新寫進的屬性;

URL是字符串類型,但如果使用該屬性,會發出警告“已過時”,所以建議使用前者。

下面這個例子,點擊按鈕後都是打開WEB瀏覽器並定位到文本框中輸入的地址,但分別用了上面所說的兩個屬性,當程序運行後,你會發現其效果是一樣的。

  1. <phone:PhoneApplicationPage   
  2.     x:Class="WebTask.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.   
  16.     <!--LayoutRoot 是包含所有頁面內容的根網格-->  
  17.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  18.         <Grid.RowDefinitions>  
  19.             <RowDefinition Height="Auto"/>  
  20.             <RowDefinition Height="*"/>  
  21.         </Grid.RowDefinitions>  
  22.   
  23.         <!--TitlePanel 包含應用程序的名稱和頁標題-->  
  24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  25.             <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>  
  26.             <TextBlock x:Name="PageTitle" Text="啓動Web瀏覽器" Margin="9,-7,0,0" FontSize="50"/>  
  27.         </StackPanel>  
  28.   
  29.         <!--ContentPanel - 在此處放置其他內容-->  
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  31.             <TextBlock Height="40" HorizontalAlignment="Left" Margin="38,57,0,0" Name="textBlock1" Text="請輸入URL:" VerticalAlignment="Top" Width="286" FontSize="28"/>  
  32.             <TextBox Height="72" HorizontalAlignment="Left" Margin="12,122,0,0" Name="txtUrl" VerticalAlignment="Top" Width="394" />  
  33.             <Button Content="通過Uri對象設置並啓動" Height="79" HorizontalAlignment="Left" Margin="12,222,0,0" Name="button1" VerticalAlignment="Top" Width="394" Click="button1_Click" />  
  34.             <Button Content="通過字符串設置並啓動" Height="79" HorizontalAlignment="Left" Margin="9,323,0,0" Name="button2" VerticalAlignment="Top" Width="394" Click="button2_Click" />  
  35.         </Grid>  
  36.     </Grid>  
  37.    
  38.   
  39. </phone:PhoneApplicationPage>  
<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>


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Microsoft.Phone.Tasks;  
  14.   
  15. namespace WebTask  
  16. {  
  17.     public partial class MainPage : PhoneApplicationPage  
  18.     {  
  19.         // 構造函數   
  20.         public MainPage()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         private void button1_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             // 通過Uri對象設置   
  28.             WebBrowserTask wt = new WebBrowserTask();  
  29.             wt.Uri = new Uri(txtUrl.Text, UriKind.Absolute);  
  30.             wt.Show();  
  31.         }  
  32.   
  33.         private void button2_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             // 通過字符串設置   
  36.             WebBrowserTask wt = new WebBrowserTask();  
  37.             wt.URL = txtUrl.Text;  
  38.             wt.Show();  
  39.         }  
  40.     }  
  41. }  
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();
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章