Tile Output demonstrates how to update the Primary Tile of an Application with a ScheduledTileNotification on the Start Menu of Windows 10
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.Collections.Generic; using System.Linq; using Windows.Data.Xml.Dom; using Windows.UI.Notifications; using Windows.UI.Xaml.Controls; public class Item { public string Id { get; set; } public string Content { get; set; } public string Time { get; set; } } public class Library { private Random _random = new Random((int)DateTime.Now.Ticks); private TileUpdater _updater = TileUpdateManager.CreateTileUpdaterForApplication(); public void Init(ListBox display) { display.Items.Clear(); IReadOnlyList<ScheduledTileNotification> list = _updater.GetScheduledTileNotifications(); foreach (ScheduledTileNotification item in list) { display.Items.Add(new Item { Id = item.Id, Time = item.Content.GetElementsByTagName("text")[1].InnerText, Content = item.Content.GetElementsByTagName("text")[0].InnerText, }); } } public void Add(ref ListBox display, string value, 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) { XmlDocument xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text03); xml.GetElementsByTagName("text")[0].InnerText = value; xml.GetElementsByTagName("text")[1].InnerText = when.ToString("HH:mm"); ScheduledTileNotification tile = new ScheduledTileNotification(xml, when) { Id = _random.Next(1, 100000000).ToString() }; _updater.AddToSchedule(tile); display.Items.Add(new Item { Id = tile.Id, Content = value, Time = when.ToString() }); } } public void Remove(ListBox display) { if (display.SelectedIndex > -1) { TileUpdateManager.CreateTileUpdaterForApplication().GetScheduledTileNotifications().Where( p => p.Id.Equals(((Item)display.SelectedItem).Id)).SingleOrDefault(); display.Items.RemoveAt(display.SelectedIndex); } } }
In the Library.cs there are using statements to include the necessary functionality. There is a Member for the TileUpdater required and an Item Class to represent displayed items, an Init Method which will populate the ListBox with any ScheduledTileNotification from GetScheduledTileNotifications, an Add Method to add a ScheduledTileNotification with the selected GetTemplateContent from TileUpdateManager plus insert into the ListBox an item, the Remove Method will remove items from the ListBox.
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:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBox Grid.Column="0" Margin="10" Name="Value"/> <TimePicker Grid.Column="1" Margin="10" Name="Occurs"/> </Grid> <ListBox Name="Display" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock TextWrapping="Wrap" Text="{Binding Path=Content}"/> <TextBlock Text="{Binding Path=Time}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Add" Label="Add" Click="Add_Click"/> <AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a Grid with the first row containing a TextBox where text may be entered to show on the Pinned Application Tile and then there’s a TimePicker where the Time when to update the Pinned Application Tile may be selected. The second row contains a ListBox to display the list of Tiles or items that have been pinned to Start. The second block of XAML is the CommandBar which contains the Add and Remove options to manage the list.
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(); protected override void OnNavigatedTo(NavigationEventArgs e) { library.Init(Display); } private void Add_Click(object sender, RoutedEventArgs e) { library.Add(ref Display, Value.Text, Occurs.Time); } private void Remove_Click(object sender, RoutedEventArgs e) { library.Remove(Display); }
Below the MainPage() Method an instance of the Library Class is created. The OnNavigatedTo Event handler will call the Init Method in the Library Class. The Add_Click Method will add items to the Start Menu and also to the ListBox and Remove_Click will delete items from the list.
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 you can then type some text in the TextBox then select a Time, then select Add to schedule a Tile Notification.
Step 15
Then select the Start button on Local Machine of Windows 10 then find the app under Recently Added then tap and hold or right-click and select Pin to Start then once done wait for the Notification to appear at the time selected on the Tile for the Application
Step 16
To Exit the Application select the Close button in the top right of the Application