[英]Selecting contacts in windows phone 8
我正在嘗試將基本聯系人列表添加到我的應用中。
到目前為止,該應用程序查詢聯系人存儲並在列表中顯示所有內容。
我需要的是一個數據結構,其中包含用戶從列表中選擇的每個聯系人的姓名和電話號碼。
我很樂意看到您的想法。 我相信這將是我想念的簡單事情,但是我已經做了很多嘗試,但現在卻感到非常困惑。
這是相關的代碼片段和隨附的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>
您可以創建自己的課程
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)
{
DisplayName = contact.DisplayName;
var number = contact.PhoneNumbers.FirstOrDefault();
if(number != null)
Number = number.PhoneNumber;
else
Number = "";
}
}
然后遍歷結果並將其添加到您的課程中
List<CustomContact> listOfContacts = new List<CustomContact>();
foreach (var c in e.Results)
{
CustomContact contact = new CustomContact();
contact.DisplayName = c.DisplayName;
var number = c.PhoneNumbers.FirstOrDefault(); //change this to whatever number you want
if (number != null)
contact.Number = number.PhoneNumber;
else
contact.Number = "";
listOfContacts.Add(contact);
}
ContactResultsData.DataContext = listOfContacts;
您可以將上面的foreach
循環縮短為一個LINQ查詢
listOfContacts = e.Results.Select(x => new CustomContact()
{
Number = x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().PhoneNumber : "",
DisplayName = x.DisplayName
}).ToList();
根據評論更新。
假設您不使用上述方法,則ListBox中將填充Contact
對象(而不是我們的CustomContact
對象)。 因此,我們將所選項目轉換為Contact
對象,並使用接受了Contact
對象的重載構造函數來創建所需的CustomContact
對象。
private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Contact contact = ContactResultsData.SelectedItem as Contact;
if (contact != null)
{
CustomContact customContact = new CustomContact(contact);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.