Universal Windows Platform – Tiles

Tiles demonstrates how to Pin SecondaryTile items to 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.

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 Fall Creators Update (10.0; Build 16299) 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.Globalization;
using Windows.UI;
using Windows.UI.StartScreen;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

public class Item
{
    public string Id { get; set; }
    public string Content { get; set; }
    public Brush Colour { get; set; }
}

public class Library
{
    private Random _random = new Random((int)DateTime.Now.Ticks);

    private Color FromString(string value)
    {
        return Color.FromArgb(
        Byte.Parse(value.Substring(0, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(2, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(4, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(6, 2), NumberStyles.HexNumber));
    }

    public async void Init(ListBox display)
    {
        display.Items.Clear();
        IReadOnlyList<SecondaryTile> list = await SecondaryTile.FindAllAsync();
        foreach (SecondaryTile item in list)
        {
            display.Items.Add(new Item
            {
                Id = item.TileId,
                Content = item.DisplayName,
                Colour = new SolidColorBrush(item.VisualElements.BackgroundColor)
            });
        }
    }

    public async void Add(ListBox display, string value, ComboBox colour, object selection)
    {
        string id = _random.Next(1, 100000000).ToString();
        SecondaryTile tile = new SecondaryTile(id, value, id, new Uri("ms-appx:///"), TileSize.Default);
        Color background = FromString(((ComboBoxItem)colour.SelectedItem).Tag.ToString());
        tile.VisualElements.BackgroundColor = background;
        tile.VisualElements.ForegroundText = ForegroundText.Light;
        tile.VisualElements.ShowNameOnSquare150x150Logo = true;
        tile.VisualElements.ShowNameOnSquare310x310Logo = true;
        tile.VisualElements.ShowNameOnWide310x150Logo = true;
        await tile.RequestCreateAsync();
        display.Items.Add(new Item { Id = tile.TileId, Content = value, Colour = new SolidColorBrush(background) });
    }

    public async void Remove(ListBox display)
    {
        if (display.SelectedIndex > -1)
        {
            string id = ((Item)display.SelectedItem).Id;
            if (SecondaryTile.Exists(id))
            {
                SecondaryTile tile = new SecondaryTile(id);
                await tile.RequestDeleteAsync();
            }
            display.Items.RemoveAt(display.SelectedIndex);
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. The Item Class represents a pinned Tile, then in the Library Class the FromString Method converts a string colour into a Color. The Init Method sets the contents of the ListBox to a list of items by using SecondaryTile.FindAllAsync. The Add Method will not only insert an item in to the ListBox but also Pin a SecondaryTile to the Start Menu of Windows 10. Remove will un-pin the Tile and remove the entry from the ListBox.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

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:

<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="20" VerticalAlignment="Center" Name="Value"/>
		<ComboBox Grid.Column="1" Name="Colour" VerticalAlignment="Center" Margin="20">
			<ComboBoxItem Tag="FF000000" IsSelected="True">
				<Rectangle Width="25" Height="14" Fill="Black"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF808080">
				<Rectangle Width="25" Height="14" Fill="Gray"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF0000">
				<Rectangle Width="25" Height="14" Fill="Red"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFA500">
				<Rectangle Width="25" Height="14" Fill="Orange"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFFF00">
				<Rectangle Width="25" Height="14" Fill="Yellow"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF008000">
				<Rectangle Width="25" Height="14" Fill="Green"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF00FFFF">
				<Rectangle Width="25" Height="14" Fill="Cyan"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF0000FF">
				<Rectangle Width="25" Height="14" Fill="Blue"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF00FF">
				<Rectangle Width="25" Height="14" Fill="Magenta"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF800080">
				<Rectangle Width="25" Height="14" Fill="Purple"/>
			</ComboBoxItem>
		</ComboBox>
	</Grid>
	<ListBox Name="Display" Grid.Row="1">
		<ListBox.ItemTemplate>
			<DataTemplate>
				<StackPanel Orientation="Horizontal">
					<TextBlock TextWrapping="Wrap" Text="{Binding Path=Content}"/>
					<Rectangle Width="25" Height="14" Margin="5" Fill="{Binding Path=Colour}"/>
				</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 a Tile and then there’s a ComboBox where the background colour of the 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

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();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	library.Init(Display);
}

private void Add_Click(object sender, RoutedEventArgs e)
{
	library.Add(Display, Value.Text, Colour, sender);
}

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

vs2017-local-machine

Step 14

After the Application has started running you can then type some text then select a Colour, then select Add to Pin to Start, it will ask “Do you want to Pin this tile to Start?”

run-tiles

Step 15

Along with an item being added to the list you can also select Start button on Local Machine of Windows 10 to view the pinned Tile

ran-tiles

Step 16

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

vs2017-close

Creative Commons License

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 )

Connecting to %s