在Windows phone 8 c#中合并相同的联系人与不同的联系人详细信息

本文关键字:联系人 详细信息 合并 phone Windows | 更新日期: 2023-09-27 18:03:34

在我的windows phone应用程序中,我将所有联系人输入到我的应用程序中,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;
namespace GetContacts
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        private void MergeContacts_Click(object sender, RoutedEventArgs e)
        {
            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted1);
            cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
        }
        private void Contacts_SearchCompleted1(object sender, ContactsSearchEventArgs e)
        {
            // MessageBox.Show(e.Results.Count().ToString());
            try
            {
               ContactResultsData.DataContext = e.Results;
            }
            catch (System.Exception)
            {
                //No results
            }
            if (ContactResultsData.Items.Any())
            {
                ContactResultsLabel.Text = "results";
            }
            else
            {
                ContactResultsLabel.Text = "no results";
            }
        }

    }
}

它的工作很好,让我所有的联系人列表,下面是我的xaml代码:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel>
                <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />
                <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
            <Button x:Name="ButtonContacts"
                    Content="Get All Contacts"
                    FontSize="15"
                    Width="200"
                    Height="70"
                    Background="AliceBlue"
                    Foreground="Blue"
                    HorizontalAlignment="Left"
                    Click="ButtonContacts_Click"></Button>
            <Button x:Name="MergeContacts"
                    Content="Merge Contacts"
                    FontSize="15"
                    Width="200"
                    Height="70"
                    Background="AliceBlue"
                    Foreground="Blue"
                    HorizontalAlignment="Right"
                    Click="MergeContacts_Click"></Button>
        </Grid>

但是当我创建相同的联系人时不同的联系人详细信息,如联系人姓名Alan联系人详细信息'手机号码1234567890',并创建另一个联系人具有相同的名称Alan和联系人详细信息phone number 923451234567,然后它显示我两个联系人具有相同的名称Alan,我想将此联系人详细信息与一个联系人名称Alan合并。

请建议我,等待你的回复。谢谢。

在Windows phone 8 c#中合并相同的联系人与不同的联系人详细信息

下面是一个例子,我从一个xml文件中读取,并在wp8

中的联系人类中添加。

你也可以做同样的修改....

你需要根据你的需求稍微修改一下LinQ查询…

//on button click event from XML file contact write to contacts

    private void adcnts_Click(object sender, RoutedEventArgs e)
    {
        aaaaaa();
        using (IsolatedStorageFile istf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream istfs = istf.OpenFile("MyContacts.xml", FileMode.Open))
            {
                XDocument doc = XDocument.Load(istfs);
                var query = from d in doc.Root.Descendants("Contacts")
                            select new
                            {
                                firtName = d.Element("name").Value,
                                mobilePhone = d.Element("phone").Value
                            };
                //Global qq = new Global();
                foreach (var po in query)
                {
                    //qq.cnts.Add(new Contactss()
                    //{
                    //    name = po.firtName,
                    //    number = po.mobilePhone
                    //});
                    saveContactTask.FirstName = po.firtName;
                    saveContactTask.MobilePhone = po.mobilePhone;
                    saveContactTask.Show();
                }
            }
        }
       // saveContactTask.Show();

    }
    public void aaaaaa()
    {
        saveContactTask = new SaveContactTask();
        saveContactTask.Completed += new EventHandler<SaveContactResult>(saveContactTask_Completed);
    }
    private void saveContactTask_Completed(object sender, SaveContactResult e)
    {
        switch (e.TaskResult)
        {
          // Logic for when the contact was saved successfully
            case TaskResult.OK:
               MessageBox.Show("Contact saved.");
                break;
         //Logic for when the task was cancelled by the user
            case TaskResult.Cancel:
                MessageBox.Show("Save cancelled.");
                break;
            //Logic for when the contact could not be saved
            case TaskResult.None:
                MessageBox.Show("Contact could not be saved.");
                break;
        }
    }
}

我希望这对你有帮助。为联系人创建一个模块,如CustomContact model。PhoneNumbers应该是一个联系人的多个号码的字符串列表,所以CustomContact模型应该是:

class CustomContact
    {
        public string Name { get; set; }
       // public List<string> Numbers { get; set; }
        public string Number { get; set; }

        public CustomContact()
        {
        }
        public CustomContact(string displayName, string phoneNumber)
        {
            this.Name = displayName;
            this.Number = phoneNumber;
        }
    }

和MainPage中的behindcode将数字定义为字符串列表,如下所示:

 public partial class MainPage : PhoneApplicationPage
{
    List<string> numbers = new List<string>();
    public MainPage()
    {
        InitializeComponent();
    }

和在Contacts_SearchCompleted1中创建一个CustomContact列表类型,如我的代码中的listOfContact。对于每个e. result,将联系人添加到号码列表中,每次清除号码列表,然后将号码添加到listOfContact列表,如下所示:

     void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
            {
                try
                {
                 List<CustomContact> listOfContact = new List<CustomContact>();
                  foreach (var c in e.Results)
                    {  
                        int count = c.PhoneNumbers.Count();
                        for (int i = 0; i < count; i++)
                         {
                            CustomContact contact = new CustomContact();
                            contact.Name = c.DisplayName;
                            contact.Number = c.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                            listOfContact.Add(contact);
                        }
                    }
                 ContactResultsData.ItemsSource = listOfContact;
                }
               catch (System.Exception)
               {
                  //No results
               }
               if (ContactResultsData.Items.Any())
               {
                    ContactResultsLabel.Text = "results";
               }
               else
               {
                    ContactResultsLabel.Text = "no results";
               }
            }

在UI XAML中你可以使用下面的代码:

<ListBox Background="White" Margin="10 10 10 10" Name="ContactResultsData" ItemsSource="{Binding listOfContacts}" Height="690">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="#a8a8a8" BorderThickness="0 1 0 0" Width="440" Margin="10 0 10 0">
                        <StackPanel Margin="12 18 12 18" Tap="StackPanel_Tap" Tag="{Binding Converter={StaticResource PhoneNumberConverter}}">
                            <TextBlock FontFamily="{StaticResource BYekan}" Name="ContactResultsName" Text="{Binding Path=Name}" />
                            <TextBlock FontFamily="{StaticResource BYekan}" Name="ContactResultsNumbers" Text="{Binding Path=Number}" Height="30" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>