When adding a newly generated image to a secondary tile, it doesn't find it initially - why and how do I fix it?
The full story
Hello, I'm working on a Windows Phone application that creates secondary live tiles on the phone's home screen.
Since there is currently no easy way to generate a secondary live tile with given XAML I do the following:
- Create a
UserControl
with given XAML and data bind it to my data - Use
WriteableBitmap
to write it after data binding to an image. - Use PNGWriter to write it to isolated storage
- Create the live tile with the generated
isostore:/
url.
This kinda works, the problem is when the tile is created it's not showing, only after time passes the tile suddenly appears correctly.
I use the following code for generation:
public static void GenerateMediumTile(DashboardViewModel source)
{
var image = new LiveTileMedium() {DataContext = source};
image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
image.Arrange(new Rect(0,0,336,336));
var bitmap = new WriteableBitmap(336,336);
bitmap.Render(image,new TranslateTransform());
bitmap.Invalidate();
using (var fileStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var file = fileStore.OpenFile("Shared/ShellContent/tile_" + source.Ticker + "_medium.png", FileMode.Create);
bitmap.WritePNG(file.AsOutputStream().AsStreamForWrite());
}
}
Then this code for creating the tile:
var largeImageFile = isostoreSharedShellcontentTile + tileImage.Ticker + "_big.png";
var oFliptile = new FlipTileData
{
Title = "",
BackTitle = "",
BackContent = "",
SmallBackgroundImage = new Uri("/Assets/Matrial/small_tile159x159.png", UriKind.RelativeOrAbsolute),
BackgroundImage = new Uri(mediumImageFile, UriKind.Absolute),
...
ShellTile.Create(new Uri(url, UriKind.Relative), oFliptile, true);
Here is what the tile looks like when I'm creating it, and then after time passes and I enter another app.
It’s just a guess, but maybe disposing
fileStore
doesn’t disposefile
— it will happen once garbage collection occurs, but until then, the file can’t be read. So: