參考文獻(xiàn):
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?
因?yàn)楹芟胪滤緳C(jī),當(dāng)面包烤好之后,就會(huì)彈出到你面前。當(dāng)有Voip Call或者一條聊天信息到來(lái)的時(shí)候,就會(huì)彈出到你的屏幕上。
Toast可以顯示在Desktop上,Windows 8 界面上,或者其他程序中。下圖是一個(gè)顯示在Desktop上的Toast。

二。 一個(gè)屏幕上最多顯示3個(gè)Toast,Toast沒(méi)有類似Tile的隊(duì)列,他們的位置是見(jiàn)空插針式的。
三。 Toast類似于Tile,都可以用于啟動(dòng)應(yīng)用程序。如果點(diǎn)擊Toast,應(yīng)用程序之前是關(guān)閉著的,那么將調(diào)用OnLaunched方法,如果程序之前是開(kāi)啟著的,將不調(diào)用該方法。另外,可以設(shè)置Toast 的 XML Dom對(duì)象中的launch屬性,可以傳遞參數(shù)給OnLaunched方法。等下有講。
四。 Toast是主動(dòng)顯示在屏幕上的,Tile是被動(dòng)的。
五。 要在Manifest里面設(shè)置Toast Capability為True。
類似于Tile,有三種方法可以實(shí)現(xiàn)Local Toast:使用Dom API, 使用String, 使用微軟提供的NotificationExtassion庫(kù)。事實(shí)證明,最后一種方便些。下面是這三種方法的具體實(shí)現(xiàn)步驟,都有注釋,可以簡(jiǎn)單了解一下流程。
第一種:使用API函數(shù):
1
void MainPage::toastByUsingAPIs()
2

{
3
//使用ToastImageAndText02模板,類似于Tile 包含一個(gè)圖片和兩段文字
4
ToastTemplateType toastType = ToastTemplateType::ToastImageAndText02;
5
//通過(guò)ToastNotificationManager獲得DOM對(duì)象
6
XmlDocument^ toastXML = ToastNotificationManager::GetTemplateContent(toastType);
7
//設(shè)置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 因?yàn)橐褂醚h(huán)的聲音,所以要在Toast屬性中加入duration屬性,并設(shè)置為long,
15
//如果注釋掉該屬性的話,那么將會(huì)使用默認(rèn)的聲音,時(shí)間也很短
16
17
//這里使用"toast"和"/toast"效果一樣
18
IXmlNode^ toastNode = toastXML->SelectSingleNode("toast");
19
20
//safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","111"); 如果這么用,不會(huì)出來(lái)Toast,也不會(huì)出現(xiàn)運(yùn)行時(shí)錯(cuò)誤。
21
//safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","short");使用默認(rèn)的聲音,時(shí)間很短
22
23
//所以,我們還是乖乖地使用long吧
24
safe_cast<XmlElement^>(toastNode)->SetAttribute("duration","long");//循環(huán)時(shí)的聲音用
25
//插入audio節(jié)點(diǎn)
26
XmlElement^ audioNode = toastXML->CreateElement("audio");
27
//設(shè)置audio屬性
28
audioNode->SetAttribute("src", "ms-winsoundevent:Notification.Looping.Alarm");
29
//這里一樣,如果是loop的聲音,必須要設(shè)置loop屬性,并且賦值為true
30
audioNode->SetAttribute("loop","true");
31
//插入子節(jié)點(diǎn)
32
toastNode->AppendChild(audioNode);
33
//這里就是點(diǎn)擊Toast時(shí),傳入的參數(shù)了
34
safe_cast<XmlElement^>(toastNode)->SetAttribute("launch","Toast");
35
36
ToastNotification^ toast = ref new ToastNotification(toastXML);
37
_toast = toast;
38
//有3種方法可以使Toast消失,用戶取消(點(diǎn)擊Toast上的關(guān)閉按鈕),超時(shí),使用ToastNotifier->Hide(toast)方法
39
_toast->Dismissed += ref new Windows::Foundation::TypedEventHandler<ToastNotification^,ToastDismissedEventArgs^>(this,&MainPage::toastDismissed);
40
//如果因?yàn)槭裁丛騎oast失敗的話會(huì)觸發(fā)該事件
41
_toast->Failed += ref new Windows::Foundation::TypedEventHandler<ToastNotification^,ToastFailedEventArgs^>(this,&MainPage::toastFalied);
42
//Show it!
43
ToastNotificationManager::CreateToastNotifier()->Show(toast);
44
}
可以看到,這種方法我們要耗費(fèi)大量的時(shí)間來(lái)設(shè)置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將其調(diào)度到UI線程,不是很理解,難道當(dāng)Dismiss事件被觸發(fā)的時(shí)候,使用了異步操作?
17
// 我的感覺(jué)是這個(gè)方法還是在UI線程之中,但是只使用outputText->Text = output是沒(méi)有結(jié)果的。
18
// 想想看應(yīng)該是異步的,當(dāng)你的Toast消失的時(shí)候,會(huì)花很大時(shí)間來(lái)處理
19
// 好吧,暫時(shí)得出這種結(jié)論,這種回掉函數(shù)都默認(rèn)為異步的。
20
Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,ref new Windows::UI::Core::DispatchedHandler([this,output]()
{
21
outputText->Text = output;
22
},CallbackContext::Any));
23
}
第二種:程序內(nèi)使用Xml string:
1
void MainPage::toastByUsingString()
2

{
3
String^ toastXmlString = "";
4
//我承認(rèn),用String也不方便,因?yàn)椋憧赡芏鄬懥艘粋€(gè)空格,或者多打了一個(gè)單引號(hào),就會(huì)導(dǎo)致你的Toast不能出來(lái)
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
}
這種方法的優(yōu)點(diǎn)是,一目了然,可以直接看出Xml結(jié)構(gòu),缺點(diǎn)是,簡(jiǎn)單的輸入錯(cuò)誤就會(huì)導(dǎo)致Toast不出現(xiàn)。
第三種:使用微軟的NotificationsExtasions庫(kù) 還是比較方便的,各種屬性直接設(shè)置就好了。但是我自己還沒(méi)試過(guò)。。。
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);
最后一點(diǎn),我們的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
}
點(diǎn)擊的結(jié)果就是:

有什么問(wèn)題大家可以盡情的提哈,我也是新手,共同提高。
posted on 2013-01-04 16:11
Dino-Tech 閱讀(2853)
評(píng)論(0) 編輯 收藏 引用