參考文獻:
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 閱讀(2852)
評論(0) 編輯 收藏 引用