在 'send to' 字段 WP8.1 中建议联系人(电子邮件)
Suggesting contacts (emails) in 'send to' field WP8.1
所以基本上我正在尝试创建 'To' 字段,该字段将建议与用户输入字符串匹配的电子邮件地址(如 link 中的屏幕)- 与创建新电子邮件的方式相同。
如果解决方案也适用于 Windows 8.1(我正在开发通用应用程序),那就太好了。
您可以使用类似下面的东西(它不是很复杂,例如不能快速处理输入,但您可以解决这个问题)。请务必将 Contacts 功能添加到您的项目中。
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[=25 中全局 UnhandledException
处理程序中的虚假异常=]
所以基本上我正在尝试创建 'To' 字段,该字段将建议与用户输入字符串匹配的电子邮件地址(如 link 中的屏幕)- 与创建新电子邮件的方式相同。 如果解决方案也适用于 Windows 8.1(我正在开发通用应用程序),那就太好了。
您可以使用类似下面的东西(它不是很复杂,例如不能快速处理输入,但您可以解决这个问题)。请务必将 Contacts 功能添加到您的项目中。
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[=25 中全局 UnhandledException
处理程序中的虚假异常=]