I am building a Windows Phone 8 version of an app that I already have for Windows Phone 7.1, which works perfectly.
Inside the Application_Deactivated
event (in App.xaml.cs
) I attempt to update the secondary tile of my app, if pinned to the start screen. Because it is a custom tile, I build it in code using a grid and by adding elements to it. So in the final step, I have something like (layoutRoot
is of type Grid
) :
layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();
WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (Stream fileStream = storage.CreateFile("/Shared/ShellContent/BackBackgroundImage.png"))
{
bitmap.SaveJpeg(fileStream, 336, 336, 0, 100);
}
}
So then I can update the Tile very easily. The problem is that when the app is running and I tap the "Windows" button, the app is successfully suspended and the Tile updated, but when I hit "back" to make it active again, a default "loading" text is shown on screen and nothing will happen. However, I have noticed that by commenting out the line bitmap.Render(layoutRoot, null);
, the app is re-activated successfully and it works fine, despite the fact that the Tile does not get updated upon suspension, as expected.
In the WP7.1 version of the app, this never happens, although the method for updating the Tile is the same. I really cannot figure out what is going on. Any comment/suggestion/advice will be appreciated.
Edit. Aplication_Deactivated
code:
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
UpdateLiveTile(); //Generate Tile content (as shown above) and update the Tile if it is pinned
ExportData(); //Writes data in a file
StartPeriodicAgent(); //Starts the periodic background agent that updates the live tile
}