• 在 WP7 中选择一个联系人并获取该联系人详细信息{转}


    Windows Phone Developer Tools 7.1 Beta (Mango) introduces several new launchers and choosers. From your WP7 application you can now choose an address, save a ringtone, retrieve contact details, etc. So, in this article I am going to talk about how to How to choose Contact and get Contact details in a Windows Phone 7 application.

    Previously we covered all WP7 Launchers and Choosers in our  Launchers and Choosers "How to" series of posts. So now it is time to update this series with a few new posts.

    NOTE: Before we begin make sure that you have installed the Windows Phone Developer Tools 7.1 Beta(Mango).

    Using AddressChooserTask to choose a Contact

    Generally AddressChooserTask launches the Contacts application and allows the user to select a contact. If the user completes the task, the Completed event is raised and the event handler is passed an AddressResult object which exposes a string containing the physical address and the display name for the selected contact. in order to use this task at first include the following namespace:

    1    using Microsoft.Phone.Tasks;

    Example:Here is how you can get DisplayName and Address related to the selected contact:

    XAML:

    1    <Button Content="Launch Contacts App" Click="btnContacts_Click"/>
    2    <TextBlock x:Name="tbName" FontSize="25"/>
    3    <TextBlock x:Name="tbAddress" FontSize="25"/>
     

    C#:

    01    AddressChooserTask addressTask;
    02    private string displayName = "Andrew Hill";
    03      
    04    // Constructor
    05    public MainPage()
    06    {
    07        InitializeComponent();
    08      
    09        addressTask = new AddressChooserTask();
    10        addressTask.Completed += new EventHandler<AddressResult>(addressTask_Completed);
    11    }
    12      
    13    private void btnContacts_Click(object sender, RoutedEventArgs e)
    14    {
    15        addressTask.Show();
    16    }
    17      
    18    void addressTask_Completed(object sender, AddressResult e)
    19    {
    20        if (e.TaskResult == TaskResult.OK)
    21        {
    22            this.displayName = e.DisplayName;
    23            this.tbName.Text = "Name: " + e.DisplayName;
    24            this.tbAddress.Text = "Address: "+ e.Address;
    25        }
    26    }
     

    tip65-00tip65-01tip65-02tip65-03

    Search for a Contact and get Contact details

    With the Mango release you can now query the Contact List and retrieve information.  Using the AddressChooserTask  gives you only limited access to the contact details, so if you want to retrieve the full set of details you should use the Contacts class.  Here is how we can search for the target Contact using SearchAsync() method(Note that FilterKind determines the kind of filtering you want, it can be PhoneNumber, DisplayName, EmailAddress, etc.):

     
    1    private void btnSearch_Click(object sender, RoutedEventArgs e)
    2    {
    3        Contacts contacts = new Contacts();
    4        contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
    5        contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
    6      
    7        //search for all contacts
    8        //contacts.SearchAsync(string.Empty, FilterKind.None, null);
    9    }

    Here is how we get the actual data using Results property of ContactsSearchEventArgs in the SearchCompleted handler. Have in mind that in some cases there are more than one e-mails,for example, so we will use FirstOrDefault() to get the first available.

    NOTE: You will have to add a reference to System.Device in order to be able to get the physical address:

    tip65-06

    You will also have to include the following namespace:

    using Microsoft.Phone.UserData;

     
    01    private string displayName = "Andrew Hill";
    02    void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    03    {
    04        foreach (var result in e.Results)
    05        {
    06            this.tbdisplayName.Text = "Name: " + result.DisplayName;
    07            this.tbEmail.Text = "E-mail address: " + result.EmailAddresses.FirstOrDefault().EmailAddress;
    08            this.tbPhone.Text = "Phone Number: " + result.PhoneNumbers.FirstOrDefault();
    09            this.tbPhysicalAddress.Text = "Address: " + result.Addresses.FirstOrDefault().PhysicalAddress.AddressLine1;
    10            this.tbWebsite.Text = "Website: " + result.Websites.FirstOrDefault();
    11        }
    12    }

    Example: To summarize, we can combine the both previous examples in a more complex one:

    Step1: At first we select a Name from the Contacts application using AddressChooserTask

    Step2: After that we will set displayName = e.DisplayName; in addressTask_Completed handler

    Step3: Finally we will use SearchAsync() method (filtering by DisplayName) to search for more details.

    完整的示例代码:

    XAML:

    01    <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    02        <Button Content="Launch Contacts App" Click="btnContacts_Click"/>
    03        <TextBlock x:Name="tbName" FontSize="25"/>
    04        <TextBlock x:Name="tbAddress" FontSize="25"/>
    05        <Button Content="Search for a Contact details" Click="btnSearch_Click"/>
    06        <TextBlock x:Name="tbdisplayName" FontSize="25"/>
    07        <TextBlock x:Name="tbEmail" FontSize="25"/>
    08        <TextBlock x:Name="tbPhone" FontSize="25"/>
    09        <TextBlock x:Name="tbPhysicalAddress" FontSize="25"/>
    10        <TextBlock x:Name="tbWebsite" FontSize="25"/>
    11    </StackPanel>
     

    C#:

    01    AddressChooserTask addressTask;
    02    private string displayName = "Andrew Hill";
    03    // Constructor
    04    public MainPage()
    05    {
    06        InitializeComponent();
    07      
    08        addressTask = new AddressChooserTask();
    09        addressTask.Completed += new EventHandler<AddressResult>(addressTask_Completed);
    10    }
    11      
    12    private void btnContacts_Click(object sender, RoutedEventArgs e)
    13    {
    14        addressTask.Show();
    15    }
    16      
    17    void addressTask_Completed(object sender, AddressResult e)
    18    {
    19        if (e.TaskResult == TaskResult.OK)
    20        {
    21            this.displayName = e.DisplayName;
    22            this.tbName.Text = "Name: " + e.DisplayName;
    23            this.tbAddress.Text = "Address: "+ e.Address;
    24        }
    25    }
    26      
    27    private void btnSearch_Click(object sender, RoutedEventArgs e)
    28    {
    29        Contacts contacts = new Contacts();
    30        contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
    31        contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
    32      
    33        //search for all contacts
    34        //contacts.SearchAsync(string.Empty, FilterKind.None, null);
    35    }
    36      
    37    void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    38    {
    39        foreach (var result in e.Results)
    40        {
    41            this.tbdisplayName.Text = "Name: " + result.DisplayName;
    42            this.tbEmail.Text = "E-mail address: " + result.EmailAddresses.FirstOrDefault().EmailAddress;
    43            this.tbPhone.Text = "Phone Number: " + result.PhoneNumbers.FirstOrDefault();
    44            this.tbPhysicalAddress.Text = "Address: " + result.Addresses.FirstOrDefault().PhysicalAddress.AddressLine1;
    45            this.tbWebsite.Text = "Website: " + result.Websites.FirstOrDefault();
    46        }
    47    }
     

    tip65-04tip65-05

    完整代码下载:

    希望本文对你有所帮助。http://www.oschina.net/question/54100_36891原文地址

  • 相关阅读:
    .net core 3.1 过滤器(Filter) 和中间件和AOP面向切面拦截器
    socket通信框架——boost asio
    远程过程调用框架——gRPC
    数据序列化工具——flatbuffer
    springboot项目启动----8080端口被占用排雷经过
    如何配置HOSTS文件
    使用线程Callable实现分段获取一个url连接的资源数据
    Socket网络编程课件
    (6)优化TCP编写 服务器端同时支持多个客户端同时访问
    SpringBoot配置属性之Security
  • 原文地址:https://www.cnblogs.com/Yukang1989/p/2695382.html
Copyright © 2020-2023  润新知