Universal Windows Platform – Tile Styles

Tile Styles demonstrates how to display the different types of Tile notifications that are possible for an application’s pinned Tile in 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.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-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
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.

vs2017-target-platform

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…

vs2017-project-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

vs2017-add-new-item-library

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 System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Core;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

public class Item
{
    public string Style { get; set; }
    public string Value { get; set; }
}

public class Library
{
    public IEnumerable<string> GetTemplates()
    {
        return Enum.GetValues(typeof(TileTemplateType)).Cast<TileTemplateType>()
        .Select(s => s.ToString()).Distinct();
    }

    private void UpdateTile(Item item)
    {
        TileTemplateType template = (TileTemplateType)Enum.Parse(typeof(TileTemplateType), item.Style);
        XmlDocument tile = TileUpdateManager.GetTemplateContent(template);
        XmlNodeList text = tile.GetElementsByTagName("text");
        if (text.Length > 0)
        {
            for (int i = 0; i < text.Length; i++)
            {
                text[i].AppendChild(tile.CreateTextNode(item.Value));
            }
        }
        XmlNodeList image = tile.GetElementsByTagName("image");
        if (image.Length > 0)
        {
            for (int i = 0; i < image.Length; i++)
            {
                image[i].Attributes.GetNamedItem("src").NodeValue =
                "Assets/Square44x44Logo.scale-200.png";
            }
        }
        TileNotification notification = new TileNotification(tile);
        string output = tile.GetXml();
        TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);
    }

    private async Task<Item> DialogAsync()
    {
        ComboBox template = new ComboBox()
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            Margin = new Thickness(5),
            ItemsSource = GetTemplates()
        };
        template.SelectedIndex = 0;
        TextBox text = new TextBox()
        {
            PlaceholderText = "Text",
            Margin = new Thickness(5)
        };
        StackPanel panel = new StackPanel()
        {
            Orientation = Orientation.Vertical
        };
        panel.Children.Add(template);
        panel.Children.Add(text);
        ContentDialog dialog = new ContentDialog()
        {
            Title = "Tile Styles",
            PrimaryButtonText = "Update",
            CloseButtonText = "Cancel",
            Content = panel
        };
        ContentDialogResult result = await dialog.ShowAsync();
        if (result == ContentDialogResult.Primary)
        {
            return new Item() { Style = (string)template.SelectedItem, Value = text.Text };
        }
        return null;
    }

    public async Task<bool> PinAsync()
    {
        bool isPinned = false;
        AppListEntry entry = (await Package.Current.GetAppListEntriesAsync()).FirstOrDefault();
        if (entry != null)
        {
            isPinned = await StartScreenManager.GetDefault().ContainsAppListEntryAsync(entry);
        }
        if (!isPinned)
        {
            isPinned = await StartScreenManager.GetDefault().RequestAddAppListEntryAsync(entry);
        }
        return isPinned;
    }

    public async void Tile()
    {
        Item result = await DialogAsync();
        if (result != null)
        {
            UpdateTile(result);
        }
    }
}

There’s a GetTemplates method to get the TileTemplateType and then return those that contain the value “Text” so that only supported templates are returned. UpdateTile takes a string “style” and “value” – the former is used to get the correct TileTemplateType based on the style passed in, the latter is used to create a node for the template to represent the value used on the Tile, a TileNotification is created from this and passed to the Update method of the TileUpdateManager with CreateTileUpdaterForApplication. The Dialog method has a ComboBox set to the templates using GetTemplates and has a TextBox to enter the value to show – the selected template and value is returned as an Item Class which has a pair of string values for each of those selections.

The PinAsync method determines if the application is pinned to start with the GetAppListEntriesAsync from the Package.Current with ContainsAppListEntryAsync from the StartScreenManager.GetDefault method and if not uses the RequestAddAppListEntryAsync to pin the application to start. The Tile method uses the Dialog method to get the Item Class that contains the selected template and value to use and this is passed to the UpdateTile method from the “result” of the Dialog.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 9

From the Menu choose View and then Designer

vs2017-view-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:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Pin" Label="Pin" Click="Pin_Click"/>
	<AppBarButton Icon="SetTile" Label="Tile" Click="Tile_Click"/>
</CommandBar>

The MainPage has a CommandBar with two AppBarButton Controls, Pin to perform Pin-to-Start and Tile which will show the Dialog to update the Application’s Tile on the Start Menu in Windows 10

Step 11

From the Menu choose View and then Code

vs2017-view-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 async void Pin_Click(object sender, RoutedEventArgs e)
{
	await library.PinAsync();
}

private void Tile_Click(object sender, RoutedEventArgs e)
{
	library.Tile();
}

Below the MainPage() Method an instance of the Library Class is created, then Pin_Click is used to call the PinAsync Method from the Library Class and Tile_Click the Tile Method.

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

vs2017-local-machine

Step 14

After the Application has started running you can then select Pin to pin-to-start the Application and then select Tile display a Dialog where you select Style from the ComboBox and Text.

ran-tile-styles

Step 15

The Tile will be displayed in Windows 10 on the Start Menu for the Application using the Template with the Text entered.

ran-tile-styles

Step 16

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Tile Styles shows how you can use the built-in templates for updating tiles in different ways, there are more options in some of the templates to product more complex layouts and this example is just the start of showing how flexable tile notifications can be and how easy it is to update them, makes a great feature of an application that needs to show something on it’s tile in a variety of different ways.

Creative Commons License

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s