本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器、锁定屏幕的、联系人的访问等。选择器列举了若干内置应用提供的相关支持;锁定屏幕展示了我们可以对锁定屏幕提供背景图像,屏幕通知等功能;联系人访问演示了对联系人的管理维护和只读访问。
一、选择器
选择器为手机内置应用对您的开发提供的相关支持,通过选择器你可以直接调用相机应用捕获一张照片到你的应用,或者调用联系人的相关资料,保存铃声等操作。
下面列举了大部分常用选择器:
1.地址选择器
2.相机拍照选择器
3.邮箱选择器
4.电话选择器
5.照片选择器
6.保存联系人选择器
7.保存电子邮件地址选择器
8.保存电话号码选择器
9.保存铃声选择器
下面代码罗列了调用方法。
[C#]
//地址选择器
void
showAddressChooserTask()
{
var
addressChooserTask =
new
AddressChooserTask();
addressChooserTask.Completed +=
new
EventHandler<AddressResult>((a, b) =>
{
if
(b.TaskResult == TaskResult.OK)
{
MessageBox.Show(b.DisplayName +
"的地址是:"
+ b.Address);
}
});
addressChooserTask.Show();
}
//相机拍照选择器
void
showCameraCaptureTask()
{
var
cameraCaptureTask =
new
CameraCaptureTask();
cameraCaptureTask.Completed +=
new
EventHandler<PhotoResult>((a, b) =>
{
if
(b.TaskResult == TaskResult.OK)
{
Image image =
new
Image();
var
bitmapImage =
new
BitmapImage();
bitmapImage.SetSource(b.ChosenPhoto);
image.Source = bitmapImage;
ContentPanel.Children.Add(image);
MessageBox.Show(b.OriginalFileName);
}
});
cameraCaptureTask.Show();
}
//邮箱选择器
void
showEmailAddressChooserTask()
{
var
emailAddressChooserTask =
new
EmailAddressChooserTask();
emailAddressChooserTask.Completed +=
new
EventHandler<EmailResult>((a, b) =>
{
if
(b.TaskResult == TaskResult.OK)
{
MessageBox.Show(b.DisplayName +
"的邮箱是:"
+ b.Email);
}
});
emailAddressChooserTask.Show();
}
//电话选择器
void
showPhoneNumberChooserTask()
{
var
phoneNumberChooserTask =
new
PhoneNumberChooserTask();
phoneNumberChooserTask.Completed +=
new
EventHandler<PhoneNumberResult>((a, b) =>
{
if
(b.TaskResult == TaskResult.OK)
{
MessageBox.Show(b.DisplayName +
"的电话是:"
+ b.PhoneNumber);
}
});
phoneNumberChooserTask.Show();
}
//照片选择器
void
showPhotoChooserTask()
{
var
photoChooserTask =
new
PhotoChooserTask();
photoChooserTask.Completed +=
new
EventHandler<PhotoResult>((a, b) =>
{
if
(b.TaskResult == TaskResult.OK)
{
Image image =
new
Image();
var
bitmapImage =
new
BitmapImage();
bitmapImage.SetSource(b.ChosenPhoto);
image.Source = bitmapImage;
ContentPanel.Children.Add(image);
MessageBox.Show(b.OriginalFileName);
}
});
photoChooserTask.Show();
}
//保存联系人选择器
void
showSaveContactTask()
{
var
saveContactTask =
new
SaveContactTask();
saveContactTask.Completed +=
new
EventHandler<SaveContactResult>((a, b) =>
{
switch
(b.TaskResult)
{
case
TaskResult.OK:
MessageBox.Show(
"保存成功。"
);
break
;
case
TaskResult.Cancel:
MessageBox.Show(
"取消保存。"
);
break
;
case
TaskResult.None:
MessageBox.Show(
"保存失败。"
);
break
;
}
});
//添加一些默认值
saveContactTask.FirstName =
"三"
;
saveContactTask.LastName =
"张"
;
saveContactTask.MobilePhone =
"13900000000"
;
saveContactTask.Show();
}
//保存电子邮件地址选择器
void
showSaveEmailAddressTask()
{
var
saveEmailAddressTask =
new
SaveEmailAddressTask();
saveEmailAddressTask.Completed +=
new
EventHandler<TaskEventArgs>((a, b) =>
{
switch
(b.TaskResult)
{
case
TaskResult.OK:
MessageBox.Show(
"保存成功。"
);
break
;
case
TaskResult.Cancel:
MessageBox.Show(
"取消保存。"
);
break
;
case
TaskResult.None:
MessageBox.Show(
"保存失败。"
);
break
;
}
});
//添加默认值
saveEmailAddressTask.Email =
"mail@163.com"
;
saveEmailAddressTask.Show();
}
//保存电话号码选择器
void
showSavePhoneNumberTask()
{
var
savePhoneNumberTask =
new
SavePhoneNumberTask();
savePhoneNumberTask.Completed +=
new
EventHandler<TaskEventArgs>((a, b) =>
{
switch
(b.TaskResult)
{
case
TaskResult.OK:
MessageBox.Show(
"保存成功。"
);
break
;
case
TaskResult.Cancel:
MessageBox.Show(
"取消保存。"
);
break
;
case
TaskResult.None:
MessageBox.Show(
"保存失败。"
);
break
;
}
});
//添加默认值
savePhoneNumberTask.PhoneNumber =
"13900000000"
;
savePhoneNumberTask.Show();
}
//保存铃声选择器
void
showSaveRingtoneTask()
{
var
saveRingtoneTask =
new
SaveRingtoneTask();
saveRingtoneTask.Completed +=
new
EventHandler<TaskEventArgs>((a, b) =>
{
switch
(b.TaskResult)
{
case
TaskResult.OK:
MessageBox.Show(
"保存成功。"
);
break
;
case
TaskResult.Cancel:
MessageBox.Show(
"取消保存。"
);
break
;
case
TaskResult.None:
MessageBox.Show(
"保存失败。"
);
break
;
}
});
//铃声文件路径
saveRingtoneTask.Source =
new
Uri(
"appdata:/烟花易冷.mp3"
);
//铃声在列表中的显示名称
saveRingtoneTask.DisplayName =
"烟花易冷"
;
saveRingtoneTask.Show();
}
二、锁定屏幕
在手机锁定屏幕界面,我们的应用可以更新背景图像和添加屏幕通知。
2.锁定屏幕背景
要使应用能够更新锁定屏幕背景图像,我们必须在应用程序清单文件中声明。右键选择打开方式,选择XML文本编辑器打开WMAppManifest.xml,在Tokens下添加扩展内容。
[XML]
<Extensions> <!--指示当前应用可以为锁定屏幕提供背景图像--> <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>
下面看看如何更新手机锁定屏幕背景为安装文件夹下的1.jpg图片。
[C#]
private async void SetLockImg() { try { //指示当前应用是否为锁定屏幕提供程序 var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication; if (!isProvider) { //设置当前应用为锁定屏幕提供程序 var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync(); //应用是否已设置为锁定屏幕提供程序 isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted; } if (isProvider) { // 设置锁定屏幕的背景图像 Windows.Phone.System.UserProfile.LockScreen.SetImageUri(new Uri("ms-appx:///1.jpg", UriKind.Absolute)); // 获取锁定屏幕的图像 var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri(); System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString()); } else { MessageBox.Show("不能提供锁定屏幕背景 "); } } catch { } }
2.锁定屏幕通知
应用可以设置通知信息并展示到锁定屏幕的通知区域,我们需要首先在应用程序清单文件中指定用于显示徽标的png图片文件。我们右键选择打开方式,选择XML文本编辑器打开WMAppManifest.xml,设定徽标为LockImage.png。修改Tokens标记的DeviceLockImageURI元素如下:
[XML]
<DeviceLockImageURI IsRelative="true" IsResource="false">LockImage.png</DeviceLockImageURI>
另外,我们还需要在清单文件中声明我们可以在锁定屏幕中显示通知。右键选择打开方式,选择XML文本编辑器打开WMAppManifest.xml,在Tokens下添加扩展内容。
[XML]
<Extensions> <!--指示当前应用可以添加屏幕通知--> <Extension ExtensionName="LockScreen_Notification_IconCount" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> <Extension ExtensionName="LockScreen_Notification_TextField" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>
设置完成后,我们就可以在锁定屏幕展示通知了,但具体的展示还需要用户在系统设置的锁定屏幕页,添加我们的应用为锁定屏幕通知提供程序。全部设置好后,只要默认的图标有通知的数值,这个时候锁定屏幕提示就会展示。
3.从锁定屏幕设置页启动应用
注册为锁定屏幕背景提供程序后,我们的应用可以从锁定屏幕设置页启动,默认打开mainpage,我们需要处理启动uri。
[C#]
//从系统锁定屏幕设置页面打开时的处理 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); string lockscreenKey = "WallpaperSettings"; string lockscreenValue = "0"; bool lockscreenValueExists = NavigationContext.QueryString.TryGetValue(lockscreenKey, out lockscreenValue); if (lockscreenValueExists) { //这里可以定义一个锁定屏幕背景增强设置的页面。 MessageBox.Show("从锁定屏幕设置打开。"); } }
三、联系人访问
手机内置的联系人数据库支持编程的方式去访问。我们有两种方式访问,一种是只读访问,还有一种是可以执行增删改查操作。
1.只读联系人访问
下面演示了通过查询返回只读联系人列表。
[C#]
//联系人数据 private void Button_Click_1(object sender, RoutedEventArgs e) { Contacts cons = new Contacts(); cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted); //参数1: 关键字;参数2:过滤类型,包含显示名称、电话号码、邮件地址、是否固定到开始屏幕等 cons.SearchAsync("139", FilterKind.PhoneNumber, "状态"); } void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { //获取到的联系人的集合 listbox1.ItemsSource = e.Results; }
2.自定义联系人存储
通过这种方式我们可以对系统联系人列表做各类增删改查操作,我们将演示如何操作。
[XAML]
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox x:Name="listbox1" Margin="0,10,10,173"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Id}"></TextBlock> <TextBlock Text=" "></TextBlock> <TextBlock Text="{Binding FamilyName}"></TextBlock> <TextBlock Text="{Binding GivenName}"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button Content="新增" HorizontalAlignment="Left" Margin="10,603,0,0" VerticalAlignment="Top" Click="Button_Click_1"/> <Button Content="修改" Margin="106,603,251,21" Click="Button_Click_2"/> <Button Content="删除" HorizontalAlignment="Left" Margin="205,603,0,0" VerticalAlignment="Top" Click="Button_Click_3"/> <TextBlock x:Name="textblock1" HorizontalAlignment="Left" Margin="24,556,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/> <Button Content="查看手机号" HorizontalAlignment="Left" Margin="291,603,0,0" VerticalAlignment="Top" Click="Button_Click_4"/> <Button Content="只读联系人访问〉" HorizontalAlignment="Left" Margin="224,533,0,0" VerticalAlignment="Top" Width="232" Click="Button_Click_5"/> </Grid>
[C#]
public partial class MainPage : PhoneApplicationPage { // 构造函数 public MainPage() { InitializeComponent(); Query(); } //新增联系人 async Task Add(string familyName, string givenName) { ContactStore store = await ContactStore.CreateOrOpenAsync(); StoredContact contact = new StoredContact(store); contact.FamilyName = familyName; contact.GivenName = givenName; //获取已知联系人属性 IDictionary<string, object> props = await contact.GetPropertiesAsync(); props.Add(KnownContactProperties.Telephone, "18600000000"); //获取扩展的联系人属性 IDictionary<string, object> extprops = await contact.GetExtendedPropertiesAsync(); extprops.Add("扩展", "扩展属性"); await contact.SaveAsync(); } //查询联系人,并绑定到列表 async void Query() { ContactStore store = await ContactStore.CreateOrOpenAsync(); ContactQueryResult result = store.CreateContactQuery(); IReadOnlyList<StoredContact> contacts = await result.GetContactsAsync(); listbox1.ItemsSource = contacts; } //查询已知属性 async void GetPropertie(StoredContact contact) { IDictionary<string, object> props = await contact.GetPropertiesAsync(); if (props.Keys.Contains(KnownContactProperties.Telephone)) { MessageBox.Show("手机:" + props[KnownContactProperties.Telephone].ToString()); } } //更新联系人 async Task Update(string Id) { try { ContactStore store = await ContactStore.CreateOrOpenAsync(); var contact = await store.FindContactByIdAsync(Id); if (contact != null) { contact.FamilyName = "赵"; contact.GivenName = "六"; await contact.SaveAsync(); } } catch (Exception e) { textblock1.Text = e.Message; } } //删除联系人 async private Task Delete(string Id) { try { ContactStore store = await ContactStore.CreateOrOpenAsync(); await store.DeleteContactAsync(Id); } catch (Exception e) { textblock1.Text = e.Message; } } //新增 private async void Button_Click_1(object sender, RoutedEventArgs e) { string[] familyNames = new string[] { "赵", "钱", "孙", "李", "周" }; string[] givenNames = new string[] {"云","权","飞燕","白","公瑾" }; Random r = new Random(); await Add(familyNames[r.Next(0, familyNames.Length)], givenNames[r.Next(0, givenNames.Length)]); Query(); } //修改 private async void Button_Click_2(object sender, RoutedEventArgs e) { var contact= listbox1.SelectedValue as StoredContact; if (contact == null) return; await Update(contact.Id); Query(); } //删除 private async void Button_Click_3(object sender, RoutedEventArgs e) { var contact = listbox1.SelectedValue as StoredContact; if (contact == null) return; await Delete(contact.Id); Query(); } //查看手机号 private void Button_Click_4(object sender, RoutedEventArgs e) { var contact = listbox1.SelectedValue as StoredContact; if (contact == null) return; GetPropertie(contact); } private void Button_Click_5(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); } }