Search code examples
c#windows-phone-8live-tile

How to Extract QueryString Value from ShellTile NavigationUri


I am creating my QueryString as follows when pinning a ShellTile to the start screen

var title = DateTime.Now.ToString();
string tileParameter = "uri=Title_" + title;

ShellTile Tile = CheckIfTileExist(tileParameter);  // Check if Tile's title has been used             
if (Tile == null)
{
    ...

    ShellTile.Create(new Uri("/MainPage.xaml?" + "uri=Title_" + title, UriKind.Relative), LiveTile);  //pass the tile parameter as the QueryString
}

And my CheckIfTileExists method

private ShellTile CheckIfTileExist(string tileUri)
{
    ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(tile => tile.NavigationUri.ToString().Contains(tileUri));
    return shellTile;
}

Everything works correctly in this. My issue is that when updating the tile's Background down the road I need to check to see if that particular tile is pinned already. I may have more than one ShellTile, and so I will need to update all of the Backgrounds accordingly. In the following code I was attempting to do this with a foreach statement, but since the CheckIfTileExists method only returns the FirstOrDefault I am only ever updating the first tile that was pinned, even though my foreach statement goes through every tile.

string tileParameter = "uri=Title_";

foreach (var _tile in ShellTile.ActiveTiles)
{                    
    ShellTile Tile = CheckIfTileExist(tileParameter);  // Check if Tile's title has been used             
    if (Tile != null)
    {
        ... update Background color
    }
}

How can I fix my loop so that it may go through every tile that is pinned and update? My idea was to extract the QueryString value, the title string value that was generated upon tile creation, and use this for tileParameter in the CheckIfTileExists method when updating. How could this be accomplished?


Solution

  • You could just use Where instead of FirstOrDefault -- that should return all the matches:

    string tileParameter = "uri=Title_";
    
    foreach (var _tile in ShellTile.ActiveTiles
        .Where(tile => tile.NavigationUri.ToString().Contains(tileParameter))
    {                    
        ... update Background color
    }