Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Give the following code a try:</p> <pre><code> WriteableBitmap bmpSmall = new WriteableBitmap(159, 159); var grid = new Grid(); grid.Width = bmpSmall.PixelWidth; grid.Height = bmpSmall.PixelHeight; var background = new Canvas(); background.Width = bmpSmall.PixelWidth; background.Height = bmpSmall.PixelHeight; SolidColorBrush backColor = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]); background.Background = backColor; var textBlock = new TextBlock(); textBlock.Width = bmpSmall.PixelWidth; textBlock.Text = "qwerty"; textBlock.FontWeight = FontWeights.Bold; textBlock.HorizontalAlignment = HorizontalAlignment.Stretch; textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.FontSize = 28; textBlock.Foreground = new SolidColorBrush(Colors.White); textBlock.TextAlignment = TextAlignment.Center; grid.Children.Add(background); grid.Children.Add(textBlock); grid.Measure(new Size(bmpSmall.PixelWidth, bmpSmall.PixelHeight)); grid.Arrange(new Rect(0,0,bmpSmall.PixelWidth, bmpSmall.PixelHeight)); grid.UpdateLayout(); bmpSmall.Render(grid, null); bmpSmall.Invalidate(); </code></pre> <p>I set the <code>TextBlock</code> width to the same as the rest of the tile, and set <code>HorizontalAlignment</code> to stretch, so that control would take up the whole width of the tile. Then I set the <code>TextAlignment</code> property to <code>TextAlignment.Center</code>, in order to center the text. Hope that helps!</p> <p>Edit: Apparently for writable bitmaps, you must do the measure/arrange/layout steps yourself in order to render controls as you would think they should be rendered. Give this updated code a try, it should work this time!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload