Toast Input demonstrates how to display a ScheduledToastNotification in Windows 10 that can accept input
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Text; using Windows.Data.Xml.Dom; using Windows.UI.Notifications; public class Library { private Random _random = new Random((int)DateTime.Now.Ticks); private ToastNotifier _notifier = ToastNotificationManager.CreateToastNotifier(); public void Add(TimeSpan occurs) { DateTime when = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, occurs.Hours, occurs.Minutes, occurs.Seconds); if (when > DateTime.Now) { StringBuilder template = new StringBuilder(); template.Append("<toast><visual version='2'><binding template='ToastText02'><text id='2'>Enter Message:</text></binding></visual>"); template.Append("<actions><input id='message' type='text'/><action activationType='foreground' content='Ok' arguments='ok'/></actions></toast>"); XmlDocument xml = new XmlDocument(); xml.LoadXml(template.ToString()); ScheduledToastNotification toast = new ScheduledToastNotification(xml, when) { Id = _random.Next(1, 100000000).ToString() }; _notifier.AddToSchedule(toast); } } }
In the Library.cs there are using statements to include the necessary functionality. There is a Member for the ToastNotifier and then an Add Method that takes a TimeSpan as a Parameter and will be used to create a ScheduledToastNotification based on a customised template at the specified Time and will accept some text as input using AddToSchedule.
Step 8
In the Solution Explorer select App.xaml
Step 9
From the Menu choose View and then Code
Step 10
Once in the Code View below private void OnSuspending(object sender, SuspendingEventArgs e) { … } the following should be entered:
protected async override void OnActivated(IActivatedEventArgs e) { base.OnActivated(e); try { if (e.Kind == ActivationKind.ToastNotification) { ToastNotificationActivatedEventArgs toastArgs = (ToastNotificationActivatedEventArgs)e; await new Windows.UI.Popups.MessageDialog((string)toastArgs.UserInput["message"]).ShowAsync(); } } catch { } Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e); } Window.Current.Activate(); }
The OnActivated Event Handler will be triggered when the Toast Notification Launches the application which is ActivationKind.ToastNotification the ToastNotificationActivatedEventArgs will be obtained and used to get the UserInput from the Toast to display a message on screen with its in a MessageDialog.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<TimePicker Name="Occurs" HorizontalAlignment="Center" VerticalAlignment="Center"/> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Add" Label="Add" Click="Add_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a TimePicker to set when the Toast Notification will be displayed. The second block of XAML is the CommandBar which contains the Add option.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void Add_Click(object sender, RoutedEventArgs e) { library.Add(Occurs.Time); }
Below the MainPage() Method an instance of the Library Class is created. The Add_Click Method will schedule Toast Notification to be displayed in the Action Centre in Windows 10.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then enter a Time then select Add to schedule the Toast Notification when it is displayed itt can be used to enter text to show message in the Application when it is launched in a MessageDialog
Step 15
To Exit the Application select the Close button in the top right of the Application