Tag Archives: Toast Launch

Adding Toast Launch Parameters

When users click or tap your toast notification, Windows loads your
application, and users expect your application to have information about the
context of the notification and display the appropriate information. For
example, if users see a notification that a new email has arrived, they expect
that clicking the notification will display the new email message.

To make this happen, you can and should specify the Launch property of the
toast content, when you create the toast notification. Doing this allows
Windows to pass the value of the Launch property to the event argument for
the application’s OnLaunched event, in the argument’s Arguments property.

What happens next? That’s up to you. Retrieve the Arguments property of the
OnLaunched event’s parameter, and take specific action based on the value.
You can simply pass the value along, or act on it. The sample that follows adds
a little extra code so that if the Arguments property of the parameter to the
OnLaunched event isn’t null, it passes the value along to MainPage.xaml as it
loads it. Code in the page’s OnNavigatedTo event displays the parameter it
was passed.

NOTE

If the toast notification doesn’t have its Launch property set, and if
the app is already running, Windows won’t fire the OnLaunched
event. The event always occurs if the app isn’t currently running.

Try It Out!

Follow these steps to demonstrate passing parameters from the toast to a
running application.

1. In Visual Studio, verify that you have the same sample project loaded as in
the previous demonstration, XMLToastDemo. (If you skipped that
exercise, you can start with the project in the XMLToastDemo_ex07
folder.)

2. In the Solution Explorer window, double-click MainPage.xaml.

3. In the StackPanel element, immediately before the closing element, add
the following markup:

 

Style=”{StaticResource ItemTextStyle}”/>

4. In the Solution Explorer window, right-click App.xaml and select View
Code from the context menu.

5. In the OnLaunched override, note the LaunchActivatedEventArgs
parameter—this parameter contains an Arguments property that contains
the Launch property specified in the toast notification.

6. In the OnLaunched override, immediately above the code that calls
Windows.CurrentActivate, add the following code:

7. In MainPage.xaml.cs, in the OnNavigatedTo override, add the following
code:

8. In the CreateScheduledNotification method, immediately below the code
that sets the Alt property of the image, add this code:

9. Save and run the sample.

10. Add a scheduled notification, and then return to Visual Studio and select
DEBUG|Stop Debugging (so the app is no longer running).

11. Wait for the notification, and then click it. This should load the
application, and display the parameter value (Sample parameter) in the
text block at the bottom of the controls.

12. Press ALT+F4 to shut down the application.

NOTE

This technique works if the application is still running, as well. It’s just slightly more interesting if it’s not running when the notification appears.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.