user3116680
user3116680

Reputation: 37

Operation is not valid error while creating a secondary tile

I want to add a secondary tile to the start page, but get this error when I try to create the same tile for the second time:

Operation is not valid due to the current state of the object.

IconicTileData iconicTile = new IconicTileData
{
    Title = "second page",
    IconImage = uri,
};

ShellTile.Create(new Uri("/SecondPage.xaml", UriKind.Relative),iconicTile,false);

it works for the first time i run this code, but when the tile exists and I run this code again I get that error.

Maybe I need to check whether this tile exists, but how?

Upvotes: 1

Views: 325

Answers (1)

DotNetRussell
DotNetRussell

Reputation: 9857

You are encountering a couple problems here so let me just start with the requirements

(as outlined in this near duplicate question)

  • Each Live Tile must point to a unique URI location.

  • Each time that you create a secondary tile, the user will be taken to their home screen to view it.

  • You can’t create more than one secondary tile at a time. Limit the number of secondary live tiles allowed.

You are experiencing your error because you are creating a second tile with a duplicate URI.

One way to overcome this might be to have an argument that is a random number in the URI. This will not effect the navigation and should allow you to have as many as you want.

Here is an example of this using an Override of the OnNavigatedTo event.

What should happen is when you launch the app it will create a new tile. Just click the tile and it will navigate back in to create a new tile.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
     base.OnNavigatedTo(e);

     IconicTileData iconicTile = new IconicTileData()
     {
         Title = "second page",

     };

     ShellTile.Create(new Uri("/MainPage.xaml?ran=" + (new Random().Next()).ToString(), UriKind.Relative), iconicTile, false);

}

Upvotes: 4

Related Questions