A tutorial on XML document structure of toast Notification in WIN10 application development

Source: Internet
Author: User
Tags datetime notification center

The toast notification method at the previous 8.1 is still supported in win 10, and the corresponding API protocol is included in the UWP API. Therefore, you can still use the 8.1 notification mode, that is, through the Toastnotificationmanager.gettemplatecontent (Toasttemplatetype) method to get the corresponding notification template, Then use the XmlDocument object to load and modify the XML, and of course you can construct the XML document directly and then load it with the XmlDocument object.

There is the operation center, in fact it and Toast is a gang, that is, we do not need to use a special API to operate it, because the toast notice itself can be displayed in the Operation Center.

To the WIN10 app,toast notification template is no longer divided into n a variety of, and only one, unified named Toastgeneric, that is, universal notification, this new toast notice also called adaptive toast Notice, translation for adaptive Bar.

The entire XML document is structured as follows:

<toast>
<visual>
<binding>
<text>.....</text>
........
<image .../>
........
</binding>
</visual>
<audio/>
<actions>
<input id= "Snoozetime" type= "Selection" defaultselection= "ten" >
........
</input>
<action ...>
<action .....>
</actions>
</toast>


The root node is, of course, toast, and this is nothing to explain, just as the root node of the notice of the tile is tile.

The root node contains two guys, and in fact the two brothers have described the interface structure of the toast notice.

The first part: visual, is the content area of the notification, can contain text and images.

The second part: actions, that is, under the notice can display buttons, input boxes, Drop-down select list of these can let users manipulate the control. Just like the quick reply message in WP10. After receiving the message, will display the toast notice at the top of the handset screen, then the user click the Drop-down symbol, can enter the reply information directly on the toast notice, then send.

Under the toast element, there is also a audio element that configures the sound when the toast notification is displayed, and the use of the audio element is the same as 8.1.

A binding element needs to be placed under the visual element, as before. And the focus is binding elements of the template attribute, this is different from the previous, in the UWP application, template reunification is called "toastgeneric", you do not have to consider which template, now a name.

Similarly, the binding element can contain two elements below:

<text&gt: Represents a line of text, two-text elements represent two lines of text, three represent three rows, and the first text element is usually the caption of the notification. As for the maximum number of text elements can be added, it is not sure, usually not more than four lines, after all, the notification content should not be too long. If you want to add a blank line to the content of the notification, you can write a <text/> (an empty text element) directly.

<image&gt: Represents an image. Other properties like 8.1, I'll focus on two new properties here.

>> A, placement, which is an optional attribute, if set to inline, means that the image and text are inline together; if set to Applogooverride, replace the default icon that is applied, and the image appears in the upper-left corner of the notification.

In addition, the official blog has such a sentence: You can have up to 1 image of each placement value. The values of the placement attribute can only occur once, and if there is an image element with the placement attribute of inline, then if there is an image element in the notification, then the placement property of the other element can only be applogooverride.

>> B, Hint-crop, this is also an optional property, the default is None, if set to circle, then the icon will become a circle, if you are accustomed to the square icon, then do not set this property.

Well, don't talk too much, lest we can't absorb it. Leave it to the next piece of bad writing about the actions. Let's give you a visual display of the new toast notice.

Let's show you a toast with three lines of text and an image. Look at the code:

string xml = "<toast lang=\" zh-cn\ ">" +
"<visual>" +
"<binding template=\" toastgeneric\ ">" +
"<text> High-end application </text>" +
"<text> look, peach blossom has opened." </text> "+
"<image placement=\" inline\ "src=\" ms-appx:///assets/1.jpg\ "/>" +
"<text> This peach blossom good-looking?" </text> "+
"</binding>" +
"</visual>" +
"</toast>";
Creating an XML document
XmlDocument doc = new XmlDocument ();
Loading XML
Doc. Loadxml (XML);
To create a notification instance
Toastnotification notification = new Toastnotification (DOC);
Show notifications
Toastnotifier NT = Toastnotificationmanager.createtoastnotifier ();
Nt. Show (notification);


The resulting toast notice is shown in the following figure.


Then open the Notification center, you will see the notice, click the "down" arrow below the notice, you can see the full contents of the notification.


If the notification does not appear, check the settings to see if the application notification is turned on. The following figure.


Do you want to see how it works on the phone?


Open the phone's notification center, you can also see the notice just now.


Below look at the plain text toast notification.

string xml = "<toast lang=\" zh-cn\ ">" +
"<visual>" +
"<binding template=\" toastgeneric\ ">" +
"<text> to two sentences </text>" +
"<text> wind, rain, reading, sound in the ear </text>" +
"<text> Affairs, housework, the world, everything care </text>" +
"</binding>" +
"</visual>" +
"</toast>";
Creating an XML document
XmlDocument doc = new XmlDocument ();
Loading XML
Doc. Loadxml (XML);
To create a notification instance
Toastnotification notification = new Toastnotification (DOC);
Show notifications
Toastnotifier NT = Toastnotificationmanager.createtoastnotifier ();
Nt. Show (notification);

The notification effect is as follows.


Simple WP8.1 (Runtime)]toast notice to explain

9.1 Toast Notice

Toast Notice is a temporary notice on the top of the screen, is Windows Phone general pop-up short notice, the default system messages are in the form of toast notifications, such as when your mobile phone received a message, the top of the phone pop-up message is toast Notice, Click the notice you can go directly to the details of the text page, the notice shows that the time is 7 seconds, 7 seconds will automatically disappear, if you want to quickly close the notice, you can use the toast notice above the right to slide the gesture can quickly close off the current toast notice. In addition to the system's use of such toast notifications, Third-party applications can also use this form of notification, toast notification can be not only when the application is opened, but also in the case of the application shutdown timing notification or push notification to send, This is also the toast notice of the greatest charm lies. Toast notifications should only be used for information that is of particular interest to the user, usually involving some form of user selection. Therefore, it is a good choice to receive the IM chat request and the information the user chooses to receive. However, when you consider using toast notifications, you must recognize that a very important point, because of its transient nature or because of user settings, the user may miss and not see it. Toast notifications are designed to be used in conjunction with lock-screen reminders, tile notifications, and UI applications, designed to give users instant insight into the relevant events or projects in your application. The implementation of toast notifications can also be divided into two forms, one is implemented locally in the application, the other is implemented in the cloud, the push, then our section is mainly to explain the application of the local implementation of the toast notification, in the cloud implementation of the toast notice, you can refer to the 12th chapter of the content of the push notification.

9.1.1 Create a notification message

For your application to communicate via Toast notification, you must declare that it supports Toast in the application's manifest file Package.appxmanifest, and no call to Toast notification-related APIs will not take effect. In the Package.appxmanifest visual interface, locate the "application"-> "Notifications"-> "Toast capable" and set to "Yes". Open Package.appxmanifest's code view file and you can see that the Toastcapable property of the m3:visualelements element is set to true, as shown in the following code:

<application id= "App" executable= "$targetnametoken $.exe" entrypoint= "Toastdemo.app" >

<m3:visualelements ... Toastcapable= "true" >

......

</m3:VisualElements>

</Application>

After you have added permissions for toast notifications, let's look at a code example that creates a toast notification and pops up:

Get templates for Tosat notifications

XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplatetype.toasttext01);

Locate the ' text ' element in the template and add the contents of the notification

XmlNodeList elements = toastxml.getelementsbytagname ("text");

Elements[0]. AppendChild (Toastxml.createtextnode ("A sample Toast"));

Create a toast notification by using the notification template

Toastnotification toast = new Toastnotification (toastxml);

Pop-up notification

Toastnotificationmanager.createtoastnotifier (). Show (toast);

Next, we will explain the programming steps of Toast notification step-by-step according to the above code:

(1) Template for toast notification

Each toast notification format corresponds to an XML template, before we create a toast notification object, we first need to select the toast notification template. The template for toast Notifications is described by Toasttemplatetype. The corresponding Toast notification template can be obtained by toast the static method gettemplatecontent that notifies the management class Toastnotificationmanager class. The two types of templates, ToastImageAndText01 and ToastImageAndText02, are mainly used in Windows Phone applications.

1 ToastText01 template representation is one of the simplest toast notification templates, with only notification content information, and its XML format looks like this:

<toast>

<visual>

<binding template= "ToastText01" >

<text id= "1" >bodyText</text>

</binding>

</visual>

</toast>

2 The ToastText02 template representation is a template that contains the header and the body of the message, the header is a bold text string, the message header and the message experience are separated by a space, and its XML format looks like this:

<toast>

<visual>

<binding template= "ToastText02" >

<text id= "1" >headlineText</text>

<text id= "2" >bodyText</text>

</binding>

</visual>

</toast>

(2) Add the contents of the toast notification

After obtaining the template object for the toast notification, we can modify the contents of the XML by using the XML object XmlDocument the related properties and methods of the object, thereby enabling the content information of the message to be added on the XML template of the toast notification.

(3) Create an object for toast notification

After adding the contents of the toast notification, we can use the XmlDocument object to initialize a toast notification object, at which point the Toastnotification class's construction method can be used Toastnotification ( XmlDocument content) method to initialize, which is also the only construction method of toast notification.

(4) Pop-up toast notification

Pop-up toast notification can use the Show method of the Toastnotifier class, Toastnotifier class is the notification action Manager that represents the toast notification, which enables you to obtain a toast list, open toast notification, and remove toast notifications.

9.1.2 Regular Toast notice

Toast notifications can be ejected not only when the application is running, but also when the application leaves the foreground, which can be implemented using regular toast. Periodic toast notice is by default to the future of a time, at this point in time to pop-up toast notification, if the application is not in the foreground, toast notice can also run, the user clicks Toast Notice can directly enter the current application.

Scheduledtoastnotification class representations are information classes that are periodic toast notifications, and you can use the constructor method Scheduledtoastnotification (XmlDocument content, DateTimeOffset deliverytime) method to create a Scheduledtoastnotification object, which is then added to the timing plan of the toast notification, where the content parameter represents the XML content of the message. Deliverytime represents the time when a message pops up. The sample code looks like this:

To create a ToastText02 message template

XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplatetype.toasttext02);

Get the text element of an XML template

XmlNodeList toastnodelist = toastxml.getelementsbytagname ("text");

Set up notification header information

Toastnodelist.item (0). AppendChild (Toastxml.createtextnode ("Toast title"));

Set up notification body information

Toastnodelist.item (1). AppendChild (Toastxml.createtextnode ("Toast content"));

Get a distance and now there's a 10 seconds point of time.

DateTime starttime = DateTime.Now.AddSeconds (10);

Create a Scheduledtoastnotification object using the XML template and the time of the notification

Scheduledtoastnotification recurringtoast = new Scheduledtoastnotification (Toastxml, starttime);

Set the ID of a notification

Recurringtoast.id = "ScheduledToast1";

Add a timed toast notice to the notification plan

Toastnotificationmanager.createtoastnotifier (). Addtoschedule (Recurringtoast);

9.1.3 Example Demo: Toast Notice

Here's an example of toast notification: Implement ToastText01 and ToastText02 two templates and timed notifications.

Code Listing 9-1:TOAST Notification (9th Chapter \examples_9_1)


MainPage.xaml File Main code
--------------------------------------------------------------------------------------------------------------- ---
<StackPanel>
<button content= "ToastText01 template Notification" x:name= "toastText01" click= "Toasttext01_click" width= "370" ></Button>
<button content= "ToastText02 template Notification" x:name= "toastText02" click= "Toasttext02_click" width= "370" ></Button>
<button content= "XML template Notification" X:name= "Toastxml" click= "Toastxml_click" width= "370" ></Button>
<button content= "timed notification" x:name= "Scheduledtoast" click= "Scheduledtoast_click" width= "370" ></Button>
<textblock x:name= "Info" ></TextBlock>
</StackPanel>


MainPage.xaml.cs File Main code
--------------------------------------------------------------------------------------------------------------- ---
Toast notifications for pop-up ToastText01 templates
private void Toasttext01_click (object sender, RoutedEventArgs e)
{
XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplatetype.toasttext01);
XmlNodeList elements = toastxml.getelementsbytagname ("text");
Elements[0]. AppendChild (Toastxml.createtextnode ("Hello Windows Phone 8.1"));
Toastnotification toast = new Toastnotification (toastxml);
Toast. Activated + = toast_activated;
Toast. Dismissed + = toast_dismissed;
Toast. Failed + = toast_failed;
Toastnotificationmanager.createtoastnotifier (). Show (toast);
}
Toast notifications for pop-up ToastText02 templates
private void Toasttext02_click (object sender, RoutedEventArgs e)
{
XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplatetype.toasttext02);
XmlNodeList elements = toastxml.getelementsbytagname ("text");
Elements[0]. AppendChild (Toastxml.createtextnode ("WP8.1"));
ELEMENTS[1]. AppendChild (Toastxml.createtextnode ("Hello Windows Phone 8.1"));
Toastnotification toast = new Toastnotification (toastxml);
Toast. Activated + = toast_activated;
Toast. Dismissed + = toast_dismissed;
Toast. Failed + = toast_failed;
Toastnotificationmanager.createtoastnotifier (). Show (toast);
}
A toast notification that uses XML strings to stitch out ToastText02 templates directly
private void Toastxml_click (object sender, RoutedEventArgs e)
{
String toastxmlstring = "<toast>"
+ "<visual>"
+ "<binding template= ' ToastText02 ' >"
+ "<text id= ' 1 ' >WP8.1</text>"
+ "<text id= ' 2 ' >" + "Received:" + DateTime.Now.ToLocalTime () + "</text>"
+ "</binding>"
+ "</visual>"
+ "</toast>";
XmlDocument toastxml = new XmlDocument ();
Toastxml.loadxml (toastxmlstring);
Toastnotification toast = new Toastnotification (toastxml);
Toast. Activated + = toast_activated;
Toast. Dismissed + = toast_dismissed;
Toast. Failed + = toast_failed;
Toastnotificationmanager.createtoastnotifier (). Show (toast);
}
Toast Notification Popup failed event
Async void toast_failed (toastnotification sender, Toastfailedeventargs args)
{
Await this. Dispatcher.runasync (Coredispatcherpriority.normal, () =>
{
Info. Text = "Toast notification failed:" + args. Errorcode.message;
});
}
Toast notification disappears event, when notification automatically disappears or manual cancellation triggers the event
Async void toast_dismissed (toastnotification sender, Toastdismissedeventargs args)
{
Await this. Dispatcher.runasync (Coredispatcherpriority.normal, () =>
{
Info. Text = "Toast notification disappears:" + args. Reason.tostring ();
});
}
Toast notification activates the event, and when the notification pops up, clicking on the notification triggers the event
Async void toast_activated (toastnotification sender, Object args)
{
Await this. Dispatcher.runasync (Coredispatcherpriority.normal, () =>
{
Info. Text = "Toast notification activation";
});
}
Timed Toast Notification
private void Scheduledtoast_click (object sender, RoutedEventArgs e)
{
XmlDocument toastxml = toastnotificationmanager.gettemplatecontent (toasttemplatetype.toasttext02);
XmlNodeList toastnodelist = toastxml.getelementsbytagname ("text");
Toastnodelist.item (0). AppendChild (Toastxml.createtextnode ("Toast title"));
Toastnodelist.item (1). AppendChild (Toastxml.createtextnode ("Toast content"));
DateTime starttime = DateTime.Now.AddSeconds (3);
Scheduledtoastnotification recurringtoast = new Scheduledtoastnotification (Toastxml, starttime);
Recurringtoast.id = "ScheduledToast1";
Toastnotificationmanager.createtoastnotifier (). Addtoschedule (Recurringtoast);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.