与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)
原文:与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之后台任务
- Alarm - 警报
- Reminder - 提醒
示例
1、演示 Alarm(按一个时间计划弹出警报信息)
AlarmDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.BackgroundTask.AlarmDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical">
<TextBlock Text="Alarm 的样式" />
<Image Source="/BackgroundTask/Alarm.png" /> <Button x:Name="btnRegister" Content="注册一个一分钟后启动的 Alarm" Click="btnRegister_Click" />
<TextBlock x:Name="lblMsg" />
</StackPanel> </phone:PhoneApplicationPage>
AlarmDemo.xaml.cs
/*
* ScheduledAction - 所有计划活动的基类,抽象类。ScheduledNotification 和 ScheduledTask 继承自此类
* ScheduledNotification - 用于按时间计划弹出信息,抽象类
*
* Alarm - 按一个时间计划弹出警报信息,每一个程序在某个时刻最多只能有 50 个警报信息。Alarm 继承自 ScheduledNotification
* Name - Alarm 的名称,此名称即 ID
* Title - 警报的标题,这个只能显示系统默认值,无法修改
* Content - 警报的详细内容
* Sound - 警报的警报音的地址(Uri 类型)
* BeginTime - 在此时间点弹出警报信息(系统每隔一分钟会统一调度所有 ScheduledNotification 一次,也就是说系统会在 BeginTime 所指定时间点的一分钟之内弹出相关信息)
* ExpirationTime - 警报的过期时间。当弹出警报警报后,如果用户选择了“推迟”,则一段时间过后还会继续弹出此次计划的警报信息,但是在此值所指定的时间点过后则永远不再弹出此次计划的信息
* RecurrenceType - 弹出信息的时间计划类型。Microsoft.Phone.Scheduler.RecurrenceInterval 枚举:None|Daily|Weekly|Monthly|EndOfMonth|Yearly
* IsEnabled - 目前此值无用
* IsScheduled - 此 ScheduledAction 之后是否有执行计划(只读字段)
*
* ScheduledActionService - 管理 ScheduledAction 的类
* ScheduledActionService.GetActions<T>() where T : ScheduledAction - 查找系统中已注册的 ScheduledAction 类型的数据
* ScheduledAction.Find(string name) - 按名称查找指定的 ScheduledAction
* ScheduledAction.Remove(string name) - 按名称删除指定的 ScheduledAction
* ScheduledAction.Add(ScheduledAction action) - 注册一个新的 ScheduledAction
* ScheduledAction.Replace(ScheduledAction action) - 更新指定的 ScheduledAction
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Phone.Scheduler; namespace Demo.BackgroundTask
{
public partial class AlarmDemo : PhoneApplicationPage
{
public AlarmDemo()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(AlarmDemo_Loaded);
} void AlarmDemo_Loaded(object sender, RoutedEventArgs e)
{
ShowRegisteredAlarm();
} // 显示程序中已有的 Alarm
private void ShowRegisteredAlarm()
{
// IEnumerable<ScheduledNotification> notifications = ScheduledActionService.GetActions<ScheduledNotification>();
IEnumerable<Alarm> alarms = ScheduledActionService.GetActions<Alarm>();
lblMsg.Text = "程序中已注册的 Alarm 的名称为:" + string.Join(",", alarms.Select(p => p.Name).ToList());
} private void btnRegister_Click(object sender, RoutedEventArgs e)
{
// 查找程序中指定的 Alarm,如果没有则实例化一个
Alarm alarm = ScheduledActionService.Find("alarm") as Alarm;
if (alarm == null)
alarm = new Alarm("alarm"); // alarm.Title = "Alarm Title"; // Alarm 的 Title 属性无法修改
alarm.Content = "Alarm Content";
alarm.Sound = new Uri("/Assets/SuperMario.mp3", UriKind.Relative);
alarm.BeginTime = DateTime.Now.AddMinutes();
alarm.ExpirationTime = DateTime.Now.AddDays();
alarm.RecurrenceType = RecurrenceInterval.Daily; ; // 程序中如果有没有指定的 Alarm,则 Add,否则 Replace
if (ScheduledActionService.Find("alarm") == null)
ScheduledActionService.Add(alarm);
else
ScheduledActionService.Replace(alarm); ShowRegisteredAlarm();
}
}
}
2、演示 Reminder(按一个时间计划弹出提示信息)
ReminderDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.BackgroundTask.ReminderDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical">
<TextBlock Text="Reminder 的样式" />
<Image Source="/BackgroundTask/Reminder.png" /> <Button x:Name="btnRegister" Content="注册一个一分钟后启动的 Reminder" Click="btnRegister_Click" />
<TextBlock x:Name="lblMsg" />
<TextBlock x:Name="lblParam" />
</StackPanel> </phone:PhoneApplicationPage>
ReminderDemo.xaml.cs
/*
* ScheduledAction - 所有计划活动的基类,抽象类。ScheduledNotification 和 ScheduledTask 继承自此类
* ScheduledNotification - 用于按时间计划弹出信息,抽象类
*
* Reminder - 按一个时间计划弹出提示信息,每一个程序在某个时刻最多只能有 50 个提示信息。Reminder 继承自 ScheduledNotification
* Name - Reminder 的名称,此名称即 ID
* Title - 提示的标题
* Content - 提示的详细内容
* BeginTime - 在此时间点弹出提示信息(系统每隔一分钟会统一调度所有 ScheduledNotification 一次,也就是说系统会在 BeginTime 所指定时间点的一分钟之内弹出相关信息)
* ExpirationTime - 提示的过期时间。当弹出提示信息后,如果用户选择了“推迟”,则一段时间过后还会继续弹出此次计划的提示信息,但是在此值所指定的时间点过后则永远不再弹出此次计划的信息
* RecurrenceType - 弹出信息的时间计划类型。Microsoft.Phone.Scheduler.RecurrenceInterval 枚举:None|Daily|Weekly|Monthly|EndOfMonth|Yearly
* NavigationUri - 单击弹出的提示框后,所链接到的目标地址(Uri 类型)
* IsEnabled - 目前此值无用
* IsScheduled - 此 ScheduledAction 之后是否有执行计划(只读字段)
*
* ScheduledActionService - 管理 ScheduledAction 的类
* ScheduledActionService.GetActions<T>() where T : ScheduledAction - 查找系统中已注册的 ScheduledAction 类型的数据
* ScheduledAction Find(string name) - 按名称查找指定的 ScheduledAction
* ScheduledAction.Remove(string name) - 按名称删除指定的 ScheduledAction
* ScheduledAction.Add(ScheduledAction action) - 注册一个新的 ScheduledAction
* ScheduledAction.Replace(ScheduledAction action) - 更新指定的 ScheduledAction
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Phone.Scheduler; namespace Demo.BackgroundTask
{
public partial class ReminderDemo : PhoneApplicationPage
{
public ReminderDemo()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(AlarmDemo_Loaded);
} protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (NavigationContext.QueryString.Count > )
{
lblParam.Text = "参数 param 的值为:" + this.NavigationContext.QueryString["param"];
lblParam.Text += Environment.NewLine;
lblParam.Text += "参数 param2 的值为:" + this.NavigationContext.QueryString["param2"];
} base.OnNavigatedTo(e);
} void AlarmDemo_Loaded(object sender, RoutedEventArgs e)
{
ShowRegisteredReminder();
} // 显示程序中已有的 Reminder
private void ShowRegisteredReminder()
{
// IEnumerable<ScheduledNotification> notifications = ScheduledActionService.GetActions<ScheduledNotification>();
IEnumerable<Reminder> reminders = ScheduledActionService.GetActions<Reminder>();
lblMsg.Text = "程序中已注册的 Reminder 的名称为:" + string.Join(",", reminders.Select(p => p.Name).ToList());
} private void btnRegister_Click(object sender, RoutedEventArgs e)
{
// 查找程序中指定的 Reminder,如果没有则实例化一个
Reminder reminder = ScheduledActionService.Find("reminder") as Reminder;
if (reminder == null)
reminder = new Reminder("reminder"); reminder.Title = "Reminder Title";
reminder.Content = "Reminder Content";
reminder.BeginTime = DateTime.Now.AddMinutes();
reminder.ExpirationTime = DateTime.Now.AddDays();
reminder.RecurrenceType = RecurrenceInterval.Daily; ;
reminder.NavigationUri = new Uri("/BackgroundTask/ReminderDemo.xaml?param=abc¶m2=xyz", UriKind.Relative); // 程序中如果有没有指定的 Reminder,则 Add,否则 Replace
if (ScheduledActionService.Find("reminder") == null)
ScheduledActionService.Add(reminder);
else
ScheduledActionService.Replace(reminder); ShowRegisteredReminder();
}
}
}
OK
[源码下载]
与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)的更多相关文章
- 与众不同 windows phone (13) - Background Task(后台任务)之后台文件传输(上传和下载)
原文:与众不同 windows phone (13) - Background Task(后台任务)之后台文件传输(上传和下载) [索引页][源码下载] 与众不同 windows phone (13) ...
- 与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任务)
原文:与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任 ...
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
[源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...
- Git for Windows v2.11.0 Release Notes
homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...
- Windows 8.1 应用开发后台任务概述(Windows XAML)
说到后台任务,这是在和许多 Android 开发者聊天的时候,经常被提起的话题之一, Windows 移动平台的后台任务的形式有别与 Android 的后台 service,简单的说在 Windows ...
- 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务
[源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...
- 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制
[源码下载] 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台 ...
- 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传
[源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...
- 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel)
[源码下载] 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel) 作者:webabcd 介绍重新想象 Windows 8 Store ...
随机推荐
- nginx启动过程分析
nginx的启动过程紧紧环绕着ngx_cycle_t的结构体展开,首先通过ngx_get_options()获取命令行參数.然后通过ngx_time_init()进行时间的初始化.如全局变量ngx_c ...
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
之前做一个android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对. 上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了 我就 ...
- 02-UIKit控件、MVC
目录: 一.控件使用 二.动态类型和静态类型 三.MVC 四.UIAlertView对话框 回到顶部 一.控件使用 1 事件源,事件处理方法有一个参数传进来,那个参数就是触发这个事件的时间源. UIS ...
- 新鲜出炉的Using Qt 3D to visualize music
http://blog.qt.io/blog/2016/01/27/using-qt-3d-visualize-music/
- 最小生成树(MST)[简述][模板]
Prim(添点法) 1. 任选一点(一般选1), 作为切入点,设其与最小生成树的距离为0(实际上就是选一个点,将此树实体化),. 2. 在所有未选择的点中选出与最小生成树距离最短的, 累计其距离, 并 ...
- 在Qt中如何使用QtDesigner创建的UI文件
使用Qt有一些时间了,一直在IDE环境(qtcreator和VS2003+集成器)中使用,自然少了很多麻烦的步骤.但是在享受这种便利的同 时,我们也失去了理解更多知识背后的点滴.在IDE中,如果我们要 ...
- Troubleshooting:oVirt-engine
问题:执行./create_db_devel.sh -u postgres创建数据库时出错 描述: [root@localhost dbscripts]# ./create_db_devel.sh - ...
- Spring mvc之提交表单
表单内容: <body> <form action="user/login"> <table> <tr> <td>用户名 ...
- 使用POI来实现对Excel的读写操作
事实上我感觉直接贴代码就好了.代码里面差点儿做到每一行一个凝视.应该看起来会比較简单 代码托管在github上:https://github.com/chsj1/ExcelUtils package ...
- (转载)QT中PRO文件写法的详细介绍,很有用,很重要!
版权声明:本文为博主原创文章,未经博主允许不得转载. 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: 1. 注释从“#”开始,到 ...