在Windows Phone 8中使用不同的联系人详细信息合并相同的联系人

时间:2014-05-16 06:07:10

标签: c# windows-phone-8

在我的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,然后它会显示两个同名联系人{{1我希望将此联系人详细信息与一个联系人姓名Alan合并。

亲切地建议我,等你回复。 感谢。

2 个答案:

答案 0 :(得分:0)

以下是我从xml文件中读取并附加到wp8中的联系人类的示例

你可以通过一些修改来做同样的事情......

您需要根据您的要求稍微修改LinQ查询...

//在按钮点击事件中从xml文件中读取联系人写入联系人

    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;
        }
    }

}

答案 1 :(得分:0)

我希望这对你有所帮助。为CustomContact模型等联系人创建模块。 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后面代码中将数字定义为字符串列表,如下所示:

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

并且在Contacts_SearchCompleted1中,我的代码中的listOfContact类似于CustomContact列表类型。对于每个e.resault添加联系人到数字列表,每次清除数字列表然后添加数字到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>