我正在开发一个需要自定义联系人列表的应用程序。到目前为止,应用程序查询联系人,将它们全部显示在列表框中,允许多个选择,现在只需将所有选定的电话号码写入调试器输出。我注意到的一件事是,在同步搜索之后所有联系人都可用,因此其中一些联系人不包含电话号码。
现在,在android上,存在一个布尔运算符来过滤联系人 - 这是contact.HasPhoneNumber,让查询联系人的生活变得非常轻松......到目前为止,我找不到任何等效的windows。我真的很感激任何帮助。
以下是您的细读代码以及随附的XAML。
谢谢
contact.cs
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";
}
}
private void ContactResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Contact contact = ContactResultsData.SelectedItem as Contact;
if (contact != null)
{
CustomContact customContact = new CustomContact(contact);
}
}
public void saveContacts(object sender, RoutedEventArgs e)
{
List<CustomContact> listOfContacts = new List<CustomContact>();
listOfContacts = ContactResultsData.SelectedItems.Cast<Contact>().Select(x => new CustomContact()
{
Number = x.PhoneNumbers.FirstOrDefault() != null ? x.PhoneNumbers.FirstOrDefault().PhoneNumber : "",
Name = x.DisplayName
}).ToList();
foreach (var c in listOfContacts)
{
Debug.WriteLine(c.Number.ToString());
}
}
}
XAML
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="AppName" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="contacts" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<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>
</Grid>
修改 的
所以,我想出来了!
添加此代码:
var filteredContacts = new List<Contact>();
foreach (var contact in e.Results)
{
filteredContacts.AddRange((from phoneNumber in contact.PhoneNumbers
where phoneNumber.Kind == PhoneNumberKind.Mobile
select contact).Select(cont => (Contact)cont));
}
搜索已完成的事件处理程序按照我想要的方式过滤联系人!!