在我的 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
和联系人详细信息电话号码 923451234567
它显示了两个同名联系人 Alan
,我想将此联系人详细信息与一个联系人姓名 Alan
合并。
请建议我,等待您的回复。 谢谢。
最佳答案
这是一个例子,我从一个 xml 文件中读取并附加到 wp8 中的 tihe 联系人类中
你可以通过一些修改来做同样的事情....
您需要根据您的要求稍微修改一下 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;
}
}
}
关于c# - 在 Windows Phone 8 c# 中合并相同的联系人与不同的联系人详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23693648/