重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片

时间:2021-12-07 15:29:05

[源码下载]

重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片

作者:webabcd

介绍
重新想象 Windows 8.1 Store Apps 之其他新特性

  • CoreDispatcher 的新特性
  • “日历”的相关操作
  • 自定义锁屏时需要显示的系列图片

示例
1、演示 CoreDispatcher 在 win8.1 中的新特性
CoreDispatcherDemo.xaml.cs

/*
* 演示 CoreDispatcher 在 win8.1 中的新特性
*
* 关于几个 Core 的基础请参见:http://www.cnblogs.com/webabcd/archive/2013/11/11/3417379.html
*/ using System;
using Windows.UI.Xaml.Controls; namespace Windows81.Other
{
public sealed partial class CoreDispatcherDemo : Page
{
public CoreDispatcherDemo()
{
this.InitializeComponent(); this.Loaded += CoreDispatcherDemo_Loaded;
} async void CoreDispatcherDemo_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// CoreDispatcher - 消息调度器
Windows.UI.Core.CoreDispatcher coreDispatcher = Windows.UI.Xaml.Window.Current.Dispatcher; // 优先级从高到低排序:
// 1、本地代码中的 SendMessage
// 2、CoreDispatcherPriority.High
// 3、CoreDispatcherPriority.Normal
// 4、所有设备输入消息
// 5、CoreDispatcherPriority.Low
// 6、CoreDispatcherPriority.Idle(一般用于后台任务)
await coreDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
// 调用 coreDispatcher 所在的线程
}); // 以下为 win8.1 新增 // 获取或设置当前 CoreDispatcher 的优先级
// coreDispatcher.CurrentPriority // 当前 CoreDispatcher 中的任务是否需要让步(即任务队列中是否存在更高优先级的任务,或指定的优先级及其以上的任务)
// coreDispatcher.ShouldYield()
// coreDispatcher.ShouldYield(CoreDispatcherPriority priority)
}
}
}

2、演示“日历”的相关操作
AppointmentDemo.xaml

<Page
x:Class="Windows81.Other.AppointmentDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Other"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnAddAppointment" Content="add appointment" Click="btnAddAppointment_Click" Margin="0 10 0 0" /> <Button Name="btnShowAppointment" Content="show appointment" Click="btnShowAppointment_Click" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

AppointmentDemo.xaml.cs

/*
* 演示“日历”的相关操作
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.ApplicationModel.Appointments;
using Windows.Foundation;
using Windows.UI.Xaml.Media; namespace Windows81.Other
{
public sealed partial class AppointmentDemo : Page
{
public AppointmentDemo()
{
this.InitializeComponent();
} private async void btnAddAppointment_Click(object sender, RoutedEventArgs e)
{
// 实例化一个 Appointment 对象
var appointment = new Appointment(); // 约会的开始时间
var startTime = new DateTimeOffset(, , , , , , TimeZoneInfo.Local.GetUtcOffset(DateTime.Now));
appointment.StartTime = startTime; // 约会的主题(不能超过 255 字符)
appointment.Subject = "情人节约会"; // 约会的地点(不能超过 32768 字符)
appointment.Location = "家里"; // 约会的详细内容(不能超过 1073741823 字符)
appointment.Details = "在家里做方便面吃,庆祝情人节"; // 约会的持续时间
appointment.Duration = TimeSpan.FromMinutes(); // 约会是否会持续一整天
appointment.AllDay = false; // 提醒(提醒时间点为:约会开始时间减去此属性指定的时间),如果此属性设置为 null 则不提醒
appointment.Reminder = TimeSpan.FromMinutes(); // 约会参与者的繁忙状态(AppointmentBusyStatus 枚举)
appointment.BusyStatus = AppointmentBusyStatus.Free; // 约会的隐私程度(AppointmentSensitivity 枚举:Public, Private)
appointment.Sensitivity = AppointmentSensitivity.Private; // Uri
appointment.Uri = new System.Uri("http://webabcd.cnblogs.com"); // 约会的组织者
var organizer = new AppointmentOrganizer();
organizer.DisplayName = "webabcd"; // 组织者的显示名称(不能超过 255 字符)
organizer.Address = "email@mail.com"; // 组织者的地址(不能超过 321 字符)
// 指定约会的组织者
appointment.Organizer = organizer; // 约会参与者
var invitee = new AppointmentInvitee();
invitee.DisplayName = "abc"; // 约会参与者的显示名称(不能超过 255 字符)
invitee.Address = "email2@mail.com"; // 约会参与者的地址(不能超过 321 字符)
invitee.Role = AppointmentParticipantRole.OptionalAttendee; // 约会参与者的赴约选项
invitee.Response = AppointmentParticipantResponse.Accepted; // 约会参与者的赴约响应
// 添加一个约会参与者
// appointment.Invitees.Add(invitee); // 循环约会
var recurrence = new AppointmentRecurrence();
recurrence.Unit = AppointmentRecurrenceUnit.Daily; // 循环约会的发生频率(AppointmentRecurrenceUnit 枚举)
recurrence.Interval = ; // 循环约会的发生间隔(比如 Unit 为 AppointmentRecurrenceUnit.Daily,Interval 为 2 则代表每两天发生一次)
recurrence.Occurrences = ; // 循环约会的循环次数(null 代表不限制)
recurrence.Until = null; // 循环约会的截止时间(null 代表不限制)
recurrence.WeekOfMonth = AppointmentWeekOfMonth.First; // 循环约会发生在月中的第几周
recurrence.DaysOfWeek = AppointmentDaysOfWeek.Sunday | AppointmentDaysOfWeek.Monday; // 循环约会发生在星期几(flags 类型的枚举)
recurrence.Month = ; // 约会发生的月
recurrence.Day = ; // 约会发生的日
// 指定循环约会
// appointment.Recurrence = recurrence; var rect = GetElementRect(sender as FrameworkElement);
// 在指定的位置弹出日历对话框,用于让用户确认是否将相关的约会加入日历(返回值为约会的标识,更新及删除约会都需要用到此标识)
String appointmentId = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default);
if (appointmentId != String.Empty) // 约会标识,用于更新和删除约会
{
lblMsg.Text = "Appointment Id: " + appointmentId;
}
else
{
lblMsg.Text = "Appointment not added.";
} // 相关的对话框还有如下几个:
// await AppointmentManager.ShowReplaceAppointmentAsync(); // 更新约会
// await AppointmentManager.ShowRemoveAppointmentAsync(); // 删除约会
} private async void btnShowAppointment_Click(object sender, RoutedEventArgs e)
{
var dateToShow = new DateTimeOffset(, , , , , , TimeZoneInfo.Local.GetUtcOffset(DateTime.Now));
var duration = TimeSpan.FromDays(); // 显示指定的时间段内的日历
// 第一个参数:开始时间
// 第二个参数:从开始时间计算的时间跨度
await AppointmentManager.ShowTimeFrameAsync(dateToShow, duration);
} private Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
}
}

3、演示如何自定义锁屏时需要显示的系列图片(需要指定一个 rss 文件地址)
LockScreenWallpapers.xaml.cs

/*
* 演示如何自定义锁屏时需要显示的系列图片(需要指定一个 rss 文件地址)
*
*
* 关于设置和获取锁屏单张图片,请参见:http://www.cnblogs.com/webabcd/archive/2013/09/12/3316073.html
*
*
* 注:
* 1、本例使用的 rss 文件请参见:WebServer 项目下的 wallpapers.xml 文件
* 2、在“设置”->“锁屏界面”中可以设置是否打开锁屏图片的幻灯片显示,其中幻灯片图片提供者的名字是 rss 文件中的 title
*/ using System;
using Windows.System.UserProfile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows81.Other
{
public sealed partial class LockScreenWallpapers : Page
{
public LockScreenWallpapers()
{
this.InitializeComponent();
} private async void btnDemo_Click(object sender, RoutedEventArgs e)
{
// 指定锁屏系列图片的数据源(一个 rss 数据),并弹出对话框让用户确认
SetImageFeedResult result = await LockScreen.RequestSetImageFeedAsync(new Uri("http://localhost:39630/wallpapers.xml"));
if (result == SetImageFeedResult.Success)
{
lblMsg.Text = "指定的 url 已经被设置为锁屏系列图片的数据源";
}
else if (result == SetImageFeedResult.ChangeDisabled)
{
lblMsg.Text = "安全策略不允许显示锁屏系列图片";
}
else if (result == SetImageFeedResult.UserCanceled)
{
lblMsg.Text = "用户取消了";
} // 取消锁屏系列图片
// LockScreen.TryRemoveImageFeed();
}
}
}

OK
[源码下载]