Recently my friend Zaman Alferoz Riyal, was tweeting that he was missingRowi for Windows Phone 8 as it has the feature to share whats the song we are playing now in our Windows Phone device. Now that app is discontinued as the limited support from Twitter. There I planned to make an app for my friend. and came with this app ShareTheSong for Windows Phone 8.

Untitled-1

Then I had a chat with some friends about improvements of the app, many suggested many things. My purpose to use this app is using in start screen, as, this app is a “one tap solution to share what are you listening to” but many users did not pin this app to start screen thus the ultimate purpose goes not achieved. So I came with an idea to force pin the app to start screen while first launch of the app after download and installation.

img 2

To make this happen I need to use ApplicationSettingsof System.IO.IsolatedStorage namespace to set an Application setting user data. On first launch of the app, the settings data is not set. So my logic is pin a secondary tile of the app is check if the setting data is set or not on launch of the app (when the MainPage constructor is called) and if there is no such data, pin the secondary tile and if not, just continue with sharing process of the song.

All the things I said above, I did in public MainPage(){ }, the constructor of MainPage class. Here the code follows.

using System.IO.IsolatedStorage;

namespace ShareTheSong
{
    public partial class MainPage : PhoneApplicationPage
    {
        private IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            if (!settings.Contains("userData"))
            {
                settings.Add("userData", "1");
            }
            settings.Save();

            if (settings["userData"] == "1")
            {
                pinToStart();
            } 
        }
        private void pinToStart()
        {
            StandardTileData standardTileData = new StandardTileData();

            standardTileData.BackgroundImage = new Uri("icon.png", UriKind.Relative);

            standardTileData.Title = "Share the song";
            standardTileData.BackContent = "Tap to share what are you listening now.";
            standardTileData.BackBackgroundImage = new Uri("red_back.png", UriKind.Relative);

            ShellTile tiletopin = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("MainPage.xaml"));

            if (tiletopin == null)
            {
                MessageBox.Show("Applied settings for first time. Re-open the app to use");
                ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), standardTileData);
            }
        }
    }
}

I have already explained the logic to pinning tiles thats what I have done in, the code of my logic is

if (!settings.Contains("userData"))
{
     settings.Add("userData", "1");
}
settings.Save();

if (settings["userData"] == "1")
{
     pinToStart();
}

I use a setting variable called “userdata”, a string variable which is equal to “1″ here, which I set while the launch of the app, if there is no such data, this app will pin the secondary tile to the start screen.  Since we cant programatically do the “Pin to start” feature which comes in the drop down list on long press of the app, we use this secondary tiles feature to make it possible. This is just a trick

'pin to start' is not possible programatically.

‘pin to start” is not possible programatically.

Now lets have the look at pinning part. I am using StandardTileData class. This class provides all necessary functions to set a tile.  I am using the below code to pin the tile.

private void pinToStart()
{
	StandardTileData standardTileData = new StandardTileData();

	standardTileData.BackgroundImage = new Uri("icon.png", UriKind.Relative);

	standardTileData.Title = "Share the song";
	standardTileData.BackContent = "Tap to share what are you listening now.";
	standardTileData.BackBackgroundImage = new Uri("red_back.png", UriKind.Relative);

	ShellTile tiletopin = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("MainPage.xaml"));

	if (tiletopin == null)
	{
		MessageBox.Show("Applied settings for first time. Re-open the app to use");
		ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), standardTileData);
	}
}

standardTileData initiate StandardTileData to invoke the properties of the class.BackgroundImage property used to set a background image for the tile, so I have used new Uri("icon.png", UriKind.Relative) to get the image as URI from the project root directory and set the icon.png as the background image of the tile.Title property used to set the title to the tile, which come here in the picture

Title_tile

I have set the back content of the tile with standardTileData.BackContent = "Tap to share what are you listening now."; that shown like below in tile.

backtile

Then just for fun I am using the below image as the back background image of the tile via standardTileData.BackContent = "Tap to share what are you listening now.";

red_back

then we code ShellTile tiletopin = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("MainPage.xaml")); which means, MainPage.xamlshould be navigated on tapping of the tile.

Finally we create tile with Create() of ShellTile class by ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), standardTileData);

This above Create() will automatically quit the app and show the tile. But I want to let know users about this app will quit now and they need to restart it by the below code. Here we check whether the tile is already exist or not by checking tiletopin with null. If its null (ie, if there is no tile yet,) we have to inform users about the app is to be restarted and, we have to pin the tile there.

if (tiletopin == null)
{
	MessageBox.Show("Applied settings for first time. Re-open the app to use");
	ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), standardTileData);
}

Thats all. You are done with setting the tile. :)

See Also

Another important place to find a huge amount of Windows Phone related articles is the TechNet Wiki itself. The best entry point is Windows Phone Resources on the TechNet Wiki.