我正在尝试将基本联系人列表添加到我的应用程序中。
到目前为止,该应用程序查询联系人存储并在列表中显示所有内容。
我需要的是一个数据结构,其中包含用户从列表中选择的每个联系人的姓名和号码。
我很想看看你的想法。我相信这将是我错过的一些简单的事情,但我已经尝试了很多,我现在非常困惑。
这是相关的代码片段和随附的 XAML。非常感谢您的参与。C# 更新
namespace appNamespace
{
public partial class contact : PhoneApplicationPage
{
public class CustomContact
{
public string Name { get; set; }
public string Number { get; set; }
public CustomContact()
{
}
//CTOR that takes in a Contact object and extract the two fields we need (can add more fields)
public CustomContact(Contact contact)
{
Name = contact.DisplayName;
var number = contact.PhoneNumbers.FirstOrDefault();
if (number != null)
Number = number.PhoneNumber;
else
Number = "";
}
}
public contact()
{
InitializeComponent();
}
private void showContacts(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//Do something with the results.
MessageBox.Show(e.Results.Count().ToString());
try
{
//Bind the results to the user interface.
ContactResultsData.DataContext = e.Results;
}
catch (System.Exception)
{
//No results
}
if (ContactResultsData.Items.Any())
{
ContactResultsLabel.Text = "results";
}
else
{
ContactResultsLabel.Text = "no results";
}
}
public void saveContacts(object sender, RoutedEventArgs e)
{
List<CustomContact> listOfContacts = new List<CustomContact>();
listOfContacts = e.Results.Select(x => new CustomContact()
{
Number = x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().PhoneNumber : "",
Name = x.DisplayName
}).ToList();
}
private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Contact contact = ContactResultsData.SelectedItem as Contact;
if (contact != null)
{
CustomContact customContact = new CustomContact(contact);
}
}
}
}
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,10" >
<TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />
<ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="436" Margin="12,0" SelectionMode="Multiple" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="ContactResults" FontSize="{StaticResource PhoneFontSizeMedium}" Text="{Binding Path=DisplayName, Mode=OneWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
<Button x:Name="showButton" Content="Show Contacts" HorizontalAlignment="Left" VerticalAlignment="Top" Width="218" Height="90" Margin="0,531,0,0" Click="showContacts"/>
<Button x:Name="saveButton" Content="Save Contacts" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="238,531,0,0" Width="218" Height="90" Click="saveContacts"/>
</Grid>