I'm making a custom image in a background agent on Windows Phone 8 to display on the back of my live tile. I've hit a small problem, I cannot get my text wrapping to work.
I first create a canvas, which then holds the textblock. I've tried putting a stackpanel in between but no luck. I set TextWrapping to Wrap, I set (max)height & (max)width on all elements but no luck. I've also tried setting trimming to none.
Here's the code:
private void UpdateAppTile(string message, int stuntCount)
{
System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var can = new Canvas();
can.Background = new SolidColorBrush(Color.FromArgb(255, 255, 204, 0));
can.Width = 336;
can.Height = 336;
can.MaxHeight = 336;
can.MaxWidth = 336;
TextBlock textBlock = new TextBlock();
textBlock.FontSize = 26;
textBlock.Foreground = new SolidColorBrush(Color.FromArgb(255, 51, 102, 153));
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Padding = new Thickness(10,10,10,10);
textBlock.MaxHeight = 336;
textBlock.MaxWidth = 336;
textBlock.MaxHeight = 336;
textBlock.MaxWidth = 336;
textBlock.TextTrimming = TextTrimming.None;
textBlock.Text = message;
StackPanel stackPanel = new StackPanel();
stackPanel.MaxHeight = 336;
stackPanel.MaxWidth = 336;
stackPanel.MaxHeight = 336;
stackPanel.MaxWidth = 336;
stackPanel.Children.Add(textBlock);
can.Children.Add(stackPanel);
const string liveTilePath = "/Shared/ShellContent/live_tile.jpg";
var writeableBitmap = new WriteableBitmap(336, 336);
writeableBitmap.Render(can, null);
writeableBitmap.Invalidate();
using (
var isoFileStream = new IsolatedStorageFileStream(liveTilePath, FileMode.OpenOrCreate,
IsolatedStorageFile.GetUserStoreForApplication
()))
{
writeableBitmap.SaveJpeg(isoFileStream, 336, 336, 0, 100);
}
ShellTile appTile = ShellTile.ActiveTiles.First();
if (appTile != null)
{
FlipTileData tileData = new FlipTileData
{
BackBackgroundImage = new Uri("isostore:" + liveTilePath, UriKind.Absolute),
BackContent = "",
Title = "Set at " + DateTime.Now.ToShortTimeString()
};
tileData.Count = stuntCount;
appTile.Update(tileData);
}
});
mre.WaitOne();
NotifyComplete();
}
I also have a small problem with sometimes the image not finishing rendering/saving on time and the tile showing an older version, but I'm still looking at that.
Thanks !
Jorn