简体   繁体   中英

Creating a live tile with the Iconic Tile Template

I am trying to use the Iconic Tile Template to create a secondary live tile in my Windows Phone 8 app and I am getting an exception when creating the tile. The exception I am getting is:

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.ni.dll

Additional information: initialData can be only of type StandardTileData.

the method I use to create the live tile is:

public void PinLock(Lock item)
{
    Uri smallIcon = new Uri(item.IsLocked ? LockedIcon : UnlockedIcon, UriKind.Relative);
    Uri largeIcon = new Uri(item.IsLocked ? LockedIcon : UnlockedIcon, UriKind.Relative);

    var tileData = new IconicTileData
    {
        IconImage = largeIcon,
        SmallIconImage = smallIcon,
        Title = item.Name
    };
    string tileUri = "/MainPage.xaml?Action=LockDetails&LockId=" + item.Id;
    ShellTile.Create(new Uri(tileUri, UriKind.Relative), tileData);
}

I have the live tile type set up in the WMAppManifest.xml like this:

<PrimaryToken TokenID="MyToken" TaskName="_default">
<TemplateIconic>
    <SmallImageURI IsRelative="true" IsResource="false">Assets\lock-icon.png</SmallImageURI>
    <Count>0</Count>
    <IconImageURI IsRelative="true" IsResource="false">Assets\lock-icon.png</IconImageURI>
    <Title>Locky</Title>
    <Message>
    </Message>
    <BackgroundColor>
    </BackgroundColor>
    <HasLarge>false</HasLarge>
    <LargeContent1>
    </LargeContent1>
    <LargeContent2>
    </LargeContent2>
    <LargeContent3>
    </LargeContent3>
    <DeviceLockImageURI IsRelative="true" IsResource="false">
    </DeviceLockImageURI>
</TemplateIconic>
</PrimaryToken>
</Tokens>

I can't find any good examples of the correct way to do this and I can't find anybody else on the internet running into this exception message.

Other things I have tried:

  • First creating the secondary template using StandardTemplateTileData and then when I update it try using IconicTileData. This throws an exception.
  • First creating the secondary template using StandardTemplateTileData and then deleting it and replacing it with one using IconicTileData. This didn't work (throws the same exception as above)

Any help or good examples would be very much appreciated.

The answer is in your exception. When creating additional tiles on the start screen, only a class which derives from StandardTileData can be used. This limits you to that class or its one derived implementation, FlipTileData . IconicTileData and CycleTileData derive from the lower level ShellTitleData . This class is also the base for StandardTileData.

Some examples of creating FlipTileData can be found on the MSDN documentation , but as it derives from the same base class as your example above, you might be able to get away with simply changing the class name and patching up your XML a tiny bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM