Windows Phone開發(fā)(26):啟動器與選擇器之五
啟動器與選擇器簡單的地方在于,它們的使用方法幾乎一模一樣,從前面幾節(jié)中,我相信大家基本上都知道如何使用它們了。
這里還是哆嗦一下吧,使用啟動器和選擇器的步驟如下:
1、實例化,new一個;
2、準(zhǔn)備各參數(shù),對相關(guān)的屬性賦值;
3、Show;
4、對于啟動器,不需要這步,但選擇器有返回數(shù)據(jù),所以需要處理完成事件。
本節(jié)再舉兩例子,啟動器和選擇器就可以完成了,然后我們下一節(jié)開始,探討新的知識點。
例一:媒體播放器。
這是一個啟動器,用起來更方便。
主要屬性有:
Controls——要顯示控制按鈕,如暫集,停止等,它是一個帶了Flags特性標(biāo)記的枚舉,所以可以多個值合并,如MediaPlaybackControls.Pause | MediaPlaybackControls.Stop
Location——要播放媒體的位置,Data表示文件存放在獨立存儲中,Install表示項目中的媒體文件;
Media——要播放文件的URI;
Orientation——這個更好懂了,媒體播放器的方向, 是水平還是垂直,和頁面方向一個概念。
- <phone:PhoneApplicationPage
- x:Class="sampleApp.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 是包含所有頁面內(nèi)容的根網(wǎng)格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應(yīng)用程序的名稱和頁標(biāo)題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內(nèi)容-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button Content="啟動媒體播放器" Height="126" HorizontalAlignment="Left" Margin="31,116,0,0" Name="button1" VerticalAlignment="Top" Width="381" Click="button1_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 sampleApp
- {
- public partial class MainPage : PhoneApplicationPage
- {
- // 構(gòu)造函數(shù)
- public MainPage()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- MediaPlayerLauncher player = new MediaPlayerLauncher();
- player.Controls = MediaPlaybackControls.All;
- player.Location = MediaLocationType.Install;
- player.Media = new Uri("分飛燕.mp3", UriKind.Relative);
- player.Orientation = MediaPlayerOrientation.Portrait;
- player.Show();
- }
- }
- }
例二:搜索任務(wù)。
SearchTask類也是一個啟動器,這個家伙更簡單了,它只有一個屬性要設(shè)置——SearchQuery,就是我們要搜索的關(guān)鍵字。
- <phone:PhoneApplicationPage
- x:Class="sampleApp.Page1"
- 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="Landscape" Orientation="Landscape"
- mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="728"
- shell:SystemTray.IsVisible="True">
- <!--LayoutRoot 是包含所有頁面內(nèi)容的根網(wǎng)格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應(yīng)用程序的名稱和頁標(biāo)題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="搜索" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內(nèi)容-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <TextBox Height="72" HorizontalAlignment="Left" Margin="12,86,0,0" Name="txtKey" VerticalAlignment="Top" Width="460" />
- <Button Content="搜索" Height="72" HorizontalAlignment="Left" Margin="480,86,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_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 sampleApp
- {
- public partial class Page1 : PhoneApplicationPage
- {
- public Page1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- SearchTask searcher = new SearchTask();
- searcher.SearchQuery = txtKey.Text;
- searcher.Show();
- }
- }
- }
下一節(jié)開始,我們討論獨立存儲。
還有就是提一下建議,博客編輯器有問題,每次都這樣,***次自動保存草稿后,后面就不會保存了,編輯器內(nèi)的文本無法選定。而點擊發(fā)表時沒有反應(yīng),非得刷新頁面。