I have an app where I would like to update the live tile when the user leaves the app. First question is if this is a bad idea? If the user starts the app 20 times per day will this be a bad idea or in any way affect the background service?
Second question is if there is a limit for how long or resource intensive this might be. I guess I will put this code in OnNavigatedFrom, will the OS kill the app if the updating of live tile takes too long? I need to create an image, save it to isolated storage, read the image and then update the tile.
Really looking forward to some thoughts from you on this.
EDIT 1: The reason for me asking is that if I do it like above it works just fine. But if I exit the app immediately when it starts up I only get a black tile instead of a tile with a background image. So I get the impression that the code doesn't finish. How can I avoid that?
EDIT 2: Since I'm creating my live tile dynamically I think the problem is the loading of the background image. I load a background image and then add text to it. When the tile goes black I can still see the text so there must be something with the loading of the background image that is used as background for the tile.
Edit 3:Here is the complete code for the creation of the image that is used as a background image. I tried to simplify it a little to reduce the code.
Grid grid = new Grid();
StackPanel sp = new StackPanel();
sp.Height = 173;
sp.Width = 173;
sp.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
strBackBackground = "";
StreamResourceInfo info;
sp.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
strBackBackground = "";
info = Application.GetResourceStream(new Uri("/MyApp;component/images/Icons/livetile/metro-" + strSymbol + ".png", UriKind.Relative));
// create source bitmap for Image control (image is assumed to be alread 173x173)
WriteableBitmap wbmp3 = new WriteableBitmap(1, 1);
try
{
wbmp3.SetSource(info.Stream);
}
catch
{
}
Image img3 = new Image();
img3.Source = wbmp3;
// add Image to Grid
img3.Width = 173;
img3.Height = 173;
img3.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = 0 };
TextBlock txtTemperature = new TextBlock();
TextBlock txtTemperatureRing = new TextBlock();
txtTemperature.Foreground = new SolidColorBrush(Colors.White);
txtTemperature.Text = strTemp;
txtTemperature.TextAlignment = TextAlignment.Right;
txtTemperatureRing.Style = (Style)Application.Current.Resources["PhoneTextTitle3Style"];
txtTemperatureRing.FontFamily = new FontFamily("Segoe WP Light");
txtTemperatureRing.FontSize = 40;
txtTemperatureRing.Foreground = new SolidColorBrush(Colors.White);
txtTemperatureRing.Text = "°";
txtTemperatureRing.TextAlignment = TextAlignment.Right;
txtTemperature.FontFamily = new FontFamily("Segoe WP");
txtTemperature.FontSize = 40;
txtTemperature.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = -55 };
txtTemperature.Height = 80;
txtTemperature.Width = 135;
txtTemperatureRing.Margin = new Thickness { Left = 130, Bottom = 0, Right = 0, Top = -112 };
txtTemperatureRing.Height = 50;
txtTemperatureRing.Width = 29;
sp.Children.Add(img3);
sp.Children.Add(txtTemperature);
sp.Children.Add(txtTemperatureRing);
//call measure, arrange and updatelayout to prepare for rendering
sp.Measure(new Size(173, 173));
sp.Arrange(new Rect(0, 0, 173, 173));
sp.UpdateLayout();
grid.Children.Add(sp);
WriteableBitmap wbmp = new WriteableBitmap(173, 173);
wbmp.Render(grid, null);
wbmp.Invalidate();
//write image to isolated storage
string sIsoStorePath = @"\Shared\ShellContent\tile.png";
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//ensure directory exists
String sDirectory = System.IO.Path.GetDirectoryName(sIsoStorePath);
if (!appStorage.DirectoryExists(sDirectory))
{
appStorage.CreateDirectory(sDirectory);
}
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(sIsoStorePath, System.IO.FileMode.Create, appStorage))
{
wbmp.SaveJpeg(stream, 173, 173, 0, 100);
}
}
/// If application uses both PeriodicTask and ResourceIntensiveTask
if (task is PeriodicTask)
{
ShellTile TileToFind = ShellTile.ActiveTiles.First();
if (TileToFind != null)
{
StandardTileData NewTileData = new StandardTileData
{
BackgroundImage = new Uri("isostore:Shared/ShellContent/tile.png", UriKind.Absolute),
Title = strTitle,
Count = null,
BackTitle = (string)settings["SelectedCityName"],
BackBackgroundImage = new Uri(strBackBackground, UriKind.Relative),
BackContent = strWind + Environment.NewLine + strPrecipitation
};
TileToFind.Update(NewTileData);
}
}