参考文献:
1. 31Days of Windows 8 -- Toast Notification:http://www.jeffblankenburg.com/2012/11/10/31-days-of-windows-8-day-10-toast-notifications/
2. MSDN Toast Notification OverView:http://msdn.microsoft.com/en-us/library/windows/apps/hh779727.aspx
3. Windows Store apps[9]通知概述(Toast,Tile和Badge)by 破船: http://blog.csdn.net/beyondvincent/article/details/7854641
一。 为什么叫Toast?
因为很想吐司机,当面包烤好之后,就会弹出到你面前。当有Voip Call或者一条聊天信息到来的时候,就会弹出到你的屏幕上。
Toast可以显示在Desktop上,Windows 8 界面上,或者其他程序中。下图是一个显示在Desktop上的Toast。

二。 一个屏幕上最多显示3个Toast,Toast没有类似Tile的队列,他们的位置是见空插针式的。
三。 Toast类似于Tile,都可以用于启动应用程序。如果点击Toast,应用程序之前是关闭着的,那么将调用OnLaunched方法,如果程序之前是开启着的,将不调用该方法。另外,可以设置Toast 的 XML Dom对象中的launch属性,可以传递参数给OnLaunched方法。等下有讲。
四。 Toast是主动显示在屏幕上的,Tile是被动的。
五。 要在Manifest里面设置Toast Capability为True。
类似于Tile,有三种方法可以实现Local Toast:使用Dom API, 使用String, 使用微软提供的NotificationExtassion库。事实证明,最后一种方便些。下面是这三种方法的具体实现步骤,都有注释,可以简单了解一下流程。
第一种:使用API函数:
1
void MainPage::toastByUsingAPIs()
2

{
3
//使用ToastImageAndText02模板,类似于Tile 包含一个图片和两段文字
4
ToastTemplateType toastType = ToastTemplateType::ToastImageAndText02;
5
//通过ToastNotificationManager获得DOM对象
6
XmlDocument^ toastXML = ToastNotificationManager::GetTemplateContent(toastType);
7
//设置text和image属性
8
XmlNodeList^ toastText = toastXML->GetElementsByTagName("text");
9
XmlNodeList^ toastImage = toastXML->GetElementsByTagName("image");
10
toastText->Item(0)->InnerText = "Funny cat";
11
toastText->Item(1)->InnerText = "This cat is looks like kitty";
12
safe_cast<XmlElement^>(toastImage->Item(0))->SetAttribute("src","cat.png");
13
safe_cast<XmlElement^>(toastImage->Item(0))->SetAttribute("alt","My Cat");
14
//option code 因为要使用循环的声音,所以要在Toast属性中加入duration属性,并设置为long,
15
//如果注释掉该属性的话,那么将会使用默认的声音,时间也很短
16
17
//这里使用"toast"和"/toast"效果一样
18
IXmlNode^ toastNode = toastXML->SelectSingleNode("toast");
19
20
//safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","111"); 如果这么用,不会出来Toast,也不会出现运行时错误。
21
//safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","short");使用默认的声音,时间很短
22
23
//所以,我们还是乖乖地使用long吧
24
safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","long");//循环时的声音用
25
//插入audio节点
26
XmlElement^ audioNode = toastXML->CreateElement("audio");
27
//设置audio属性
28
audioNode->SetAttribute("src", "ms-winsoundevent:Notification.Looping.Alarm");
29
//这里一样,如果是loop的声音,必须要设置loop属性,并且赋值为true
30
audioNode->SetAttribute("loop","true");
31
//插入子节点
32
toastNode->AppendChild(audioNode);
33
//这里就是点击Toast时,传入的参数了
34
safe_cast<XmlElement^>(toastNode)->SetAttribute("launch","Toast");
35
36
ToastNotification^ toast = ref new ToastNotification(toastXML);
37
_toast = toast;
38
//有3种方法可以使Toast消失,用户取消(点击Toast上的关闭按钮),超时,使用ToastNotifier->Hide(toast)方法
39
_toast->Dismissed += ref new Windows::Foundation::TypedEventHandler<ToastNotification^,ToastDismissedEventArgs^>(this,&MainPage::toastDismissed);
40
//如果因为什么原因Toast失败的话会触发该事件
41
_toast->Failed += ref new Windows::Foundation::TypedEventHandler<ToastNotification^,ToastFailedEventArgs^>(this,&MainPage::toastFalied);
42
//Show it!
43
ToastNotificationManager::CreateToastNotifier()->Show(toast);
44
}
可以看到,这种方法我们要耗费大量的时间来设置Xml文件的属性,太麻烦了。
下面是我们的Dismissed方法:
1
void Toast::MainPage::toastDismissed(ToastNotification^ sender, ToastDismissedEventArgs^ args)
2

{
3
String^ output = "";
4
switch(args->Reason)
5
{
6
case ToastDismissalReason::ApplicationHidden:
7
output = "The app hide the toast using ToastNotifier->Hide(toast)";
8
break;
9
case ToastDismissalReason::TimedOut:
10
output = "The toast has time out";
11
break;
12
case ToastDismissalReason::UserCanceled:
13
output = "User Canceled toast";
14
break;
15
}
16
// 这里使用Dispatcher将其调度到UI线程,不是很理解,难道当Dismiss事件被触发的时候,使用了异步操作?
17
// 我的感觉是这个方法还是在UI线程之中,但是只使用outputText->Text = output是没有结果的。
18
// 想想看应该是异步的,当你的Toast消失的时候,会花很大时间来处理
19
// 好吧,暂时得出这种结论,这种回掉函数都默认为异步的。
20
Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,ref new Windows::UI::Core::DispatchedHandler([this,output]()
{
21
outputText->Text = output;
22
},CallbackContext::Any));
23
}
第二种:程序内使用Xml string:
1
void MainPage::toastByUsingString()
2

{
3
String^ toastXmlString = "";
4
//我承认,用String也不方便,因为,你可能多写了一个空格,或者多打了一个单引号,就会导致你的Toast不能出来
5
toastXmlString = L"<toast duration='long' launch='Toast'>"
6
+ "<visual version ='1'>"
7
+ "<binding template='ToastImageAndText02'>"
8
+ "<text id='1'>Funny Cat</text>"
9
+ "<text id='2'>This cat is looks like kitty</text>"
10
+ "<image id='1' src='cat.png' alt='My Cat'/>"
11
+ "</binding>"
12
+ "</visual>"
13
+ "<audio src='ms-winsoundevent:Notification.Looping.Alarm' loop='true'/>"
14
+ "</toast>";
15
16
XmlDocument^ toastDom = ref new Windows::Data::Xml::Dom::XmlDocument();
17
try
18
{
19
toastDom->LoadXml(toastXmlString);
20
outputText->Text = toastDom->GetXml();
21
auto toast = ref new ToastNotification(toastDom);
22
ToastNotificationManager::CreateToastNotifier()->Show(toast);
23
}catch(Exception^ e)
24
{
25
outputText->Text = e->Message;
26
}
27
}
这种方法的优点是,一目了然,可以直接看出Xml结构,缺点是,简单的输入错误就会导致Toast不出现。
第三种:使用微软的NotificationsExtasions库 还是比较方便的,各种属性直接设置就好了。但是我自己还没试过。。。
1
IToastText02^ toastContent = ToastContentFactory::CreateToastText02();
2
toastContent->TextHeading->Text = "Sound:";
3
toastContent->TextBodyWrap->Text = audioSrc;
4
5
toastContent->Audio->Content = audioContent;
6
7
OutputText(toastContent->GetContent());
8
9
// Create a toast, then create a ToastNotifier object to show
10
// the toast
11
auto toast = toastContent->CreateNotification();
12
13
// If you have other applications in your package, you can specify the AppId of
14
// the app to create a ToastNotifier for that application
15
ToastNotificationManager::CreateToastNotifier()->Show(toast);
最后一点,我们的App.Xaml.Cpp中的OnLuanched方法:
1
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
2

{
3
4
5
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
6
if(args->Arguments == "Toast")
7
{
8
if(!rootFrame->Navigate(TypeName(FromToast::typeid),args->Arguments))
9
{
10
throw ref new FailureException("Failed to create fromToast page");
11
}
12
}
点击的结果就是:

有什么问题大家可以尽情的提哈,我也是新手,共同提高。
posted on 2013-01-04 16:11
Dino-Tech 阅读(2844)
评论(0) 编辑 收藏 引用