Windows phone 7 动态LiveTile-使用后台任务在LiveTile上呈现自定义字体文本块

Windows phone 7 动态LiveTile-使用后台任务在LiveTile上呈现自定义字体文本块,windows-phone-7,fonts,textblock,live-tile,background-agent,Windows Phone 7,Fonts,Textblock,Live Tile,Background Agent,在liveTile上使用自定义字体资源呈现自定义文本块时遇到问题 我的项目在后台更新活动磁贴。但它应该是个性化的 我正在使用这个代码。但它不起作用,当我尝试使用嵌入式字体时,文本显示为空白 位图背景效果很好。但是字体不起作用 当我在“前台代理”中使用相同的代码时,字体显示得非常完美 Grid grid = new Grid(); // load your image StreamResourceInfo info = Application.GetResourceStream(new Uri

在liveTile上使用自定义字体资源呈现自定义文本块时遇到问题

我的项目在后台更新活动磁贴。但它应该是个性化的

我正在使用这个代码。但它不起作用,当我尝试使用嵌入式字体时,文本显示为空白 位图背景效果很好。但是字体不起作用

当我在“前台代理”中使用相同的代码时,字体显示得非常完美

Grid grid = new Grid();

// load your image 
StreamResourceInfo info = Application.GetResourceStream(new Uri("tile.jpg", UriKind.Relative));

// create source bitmap for Image control (image is assumed to be alread 173x173)
WriteableBitmap wbmp2 = new WriteableBitmap(1, 1);
wbmp2.SetSource(info.Stream);
Image img = new Image();
img.Source = wbmp2;

// add Image to Grid
grid.Children.Add(img);

TextBlock text = new TextBlock() 
{
    FontFamily = new FontFamily("/MyTaskAgent;component/Fonts/Fonts.zip#Buxton Sketch"),
    Foreground = new SolidColorBrush(Colors.Black) ,
    TextWrapping = TextWrapping.Wrap,
};

text.Text = "Test";

// this is our final image containing custom text and image
WriteableBitmap wbmp = new WriteableBitmap(173, 173);

// now render everything - this image can be used as background for tile
wbmp.Render(grid, null);
wbmp.Invalidate();

我不确定这个问题是否与发布的代码有关。我发现加载自定义字体可能需要相当长的时间,因此后台代理将在加载字体之前完成,因此不会呈现任何内容


在调用
NotifyComplete()
之前,您确定渲染已完成吗?

我遇到了类似的问题,并找到了解决方案,请查看或

我可以归结为两种 1.在进行转换之前创建使用字体的隐藏文本块,或 2.创建一个FontSource

我使用了第一个,因为现在它更简单了

textblock是隐藏的,但它确保加载嵌入的字体

在我的控制范围内,我添加了以下内容:

void Grid_Loaded(object sender, RoutedEventArgs e) {
#if SILVERLIGHT
    Grid Grid = (Grid)sender;
    AddFontLoaderTextBox(Grid, "Signs Road Features");
    AddFontLoaderTextBox(Grid, "Signs G Old");
    AddFontLoaderTextBox(Grid, "Signs G");
    AddFontLoaderTextBox(Grid, "Signs G1");
    AddFontLoaderTextBox(Grid, "Signs G2");
    AddFontLoaderTextBox(Grid, "Signs G3");
    AddFontLoaderTextBox(Grid, "Signs Info");
    AddFontLoaderTextBox(Grid, "Signs Regulatory");
    AddFontLoaderTextBox(Grid, "Signs Regulatory1");
    AddFontLoaderTextBox(Grid, "Road Manager");
    AddFontLoaderTextBox(Grid, "Signs Temporary");
    AddFontLoaderTextBox(Grid, "Road Manager");
    AddFontLoaderTextBox(Grid, "Signs Warning");
    AddFontLoaderTextBox(Grid, "Signs Warning1");
#endif
}

#if SILVERLIGHT
void AddFontLoaderTextBox(Grid Grid, string fontName) {

    TextBlock TextBlock = new TextBlock();
    TextBlock.FontFamily = new FontFamily(string.Format(
        "pack://application:,,,/ITIS.Controls.LinearViewer.Silverlight;component/Fonts/{0}.ttf#{0}", fontName));
    TextBlock.Opacity = 0; /* hide the text block, we only load it for the font to be cached */
    Grid.SetRowSpan(TextBlock, 3); /* just to ensure the text block doesn't affect the size of the first row */
    Grid.Children.Insert(0, TextBlock); /* keep underneath other children */
}
#endif

我理解。。。但是,我如何确保字体已完全加载?有回叫吗?没有。所以你得猜。你可以尝试几秒钟的人工线程块,看看是否有帮助。