此文主要用於記錄如何使用EmailAddressChooserTask,該類主要用於選擇Windows Phone系統上已經儲存的Contract資訊,如:姓名,Email。類圖如下所示:
示範例子如下,XAML:
<phone:PhoneApplicationPage
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"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
x:Class="SearchTask.MainPage"
xmlns:viewModel="clr-namespace:SearchTask.ViewModel"
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 is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<viewModel:MainPageView x:Key="ViewModel"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="My First Application" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="擷取連絡人" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" DataContext="{StaticResource ViewModel}" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<TextBox x:Name="SearchBox" Text="{Binding Contract, Mode=TwoWay}"/>
<Button x:Name="SearchBut" Content="GetContract" HorizontalAlignment="Left" VerticalAlignment="Top" Height="81" Margin="4,8,0,0" Grid.Row="1" Width="196" Background="#00C07777">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ec:CallMethodAction MethodName="GetContract" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
ViewModel:
public class MainPageView : INotifyPropertyChanged
{
private EmailAddressChooserTask emailChooser;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private string contract;
public string Contract
{
get
{
return contract;
}
set
{
contract = value;
PropertyChanged(this, new PropertyChangedEventArgs("Contract"));
}
}
public MainPageView()
{
emailChooser = new EmailAddressChooserTask();
emailChooser.Completed += new EventHandler<EmailResult>((obj, e) =>
{
//如果Error不等於Null,表明操作失敗
if (null == e.Error)
{
Contract = "Name: " + e.DisplayName + " Email: " + e.Email;
}
});
}
public void GetContract()
{
emailChooser.Show();
}
}
首次運行:
單擊GetContract按鈕:
任意選擇一條記錄:
運行結束,原始碼:http://files.cnblogs.com/PerfectSoft/EmailAddressChooserTask.zip