在'发送到'字段WP8.1中建议联系人(电子邮件)

时间:2015-04-11 18:14:02

标签: c# xaml windows-phone-8.1 contacts

所以基本上我正在尝试创建“To”字段,该字段将建议与用户输入字符串匹配的电子邮件地址(如链接中的屏幕) - 与创建新电子邮件的方式相同。 如果解决方案也可以在Windows 8.1中运行(我正在开发通用应用程序),那将会很棒。

屏幕: http://i.imgur.com/rOfanov.png

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容(它不是非常复杂,例如,不能快速处理打字,但您可以解决这个问题)。请务必将通讯录功能添加到您的项目中。

<强> XAML

<Page
    x:Class="ContactsSearch.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ContactsSearch"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
    <StackPanel>
      <TextBox x:Name="input" TextChanged="OnTextChanged" IsEnabled="false"/>
      <ListBox x:Name="candidates" Height="300">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Margin="5">
              <TextBlock Text="{Binding Name}" FontSize="23"/>
              <TextBlock Text="{Binding Email}" FontSize="15" Foreground="{StaticResource PhoneAccentBrush}"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Page>

<强>代码

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Contacts;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace ContactsSearch
{
  public sealed partial class MainPage : Page
  {
    public MainPage()
    {
      this.InitializeComponent();
      this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    ContactStore store;
    bool loading = false;

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      if (store == null)
        GetStoreAsync().GetType(); // GetType avoids compiler warning
    }

    async Task GetStoreAsync()
    {
      store = await ContactManager.RequestStoreAsync();
      input.IsEnabled = true;
      Debug.WriteLine("Ready!");
    }

    private async void OnTextChanged(object sender, TextChangedEventArgs e)
    {
      if (store == null || loading)
        return;

      string search = (sender as TextBox).Text;

      Debug.WriteLine("Looking for: '{0}'", search);
      loading = true;
      candidates.Opacity = 0.8;
      candidates.Items.Clear();

      IReadOnlyList<Contact> contacts = new List<Contact>();
      if (string.IsNullOrWhiteSpace(search))
        contacts = await store.FindContactsAsync();
      else
        contacts = await store.FindContactsAsync(search);

      foreach (var c in contacts)
      {
        var email = c.Emails.FirstOrDefault();
        if (email == null)
          continue;
        candidates.Items.Add(new ContactInfo { Name = c.DisplayName, Email = email.Address });
      }

      Debug.WriteLine("Found {0} contacts", candidates.Items.Count);
      loading = false;
      candidates.Opacity = 1;
    }
  }

  public class ContactInfo
  {
    public string Name { get; set; }
    public string Email { get; set; }
  }
}

请注意,这似乎在XAML中随机崩溃,因此您可能需要忽略 App.Xaml.cs

中全局UnhandledException处理程序中的虚假异常