windows-phone-7 - Dynamic LiveTile - 通过后台任务在 LiveTile 上渲染自定义字体 TextBlock

标签 windows-phone-7 fonts textblock live-tile background-agent

我在 liveTile 上使用自定义字体资源渲染自定义文本 block 时遇到问题?

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

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

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

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();

最佳答案

我也遇到了类似的问题,找到了解决办法,请看一下herehere

我归结为 1. 在进行转换之前创建一个使用该字体的隐藏文本 block ,或者 2.创建一个FontSource。

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

文本 block 被隐藏,但它确保加载嵌入的字体。

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

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

关于windows-phone-7 - Dynamic LiveTile - 通过后台任务在 LiveTile 上渲染自定义字体 TextBlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8689768/

相关文章:

c# - 在xaml中显示来自数据库的图片

javascript - 为网站用户添加字体到 HTML/CSS 代码(跨浏览器)

apache-flex - 如何在 Flash 中模拟 Photoshop CS4 平滑字体渲染?

C# WPF - ScrollViewer + TextBlock 问题

c# - 处理WPF中的viewbox点击事件

windows-phone-7 - 如何: Data Persistence in Windows Phone

android - 基于 Android 和 Windows Phone 的 SDL 支持

WPF 将 TextBlock 绑定(bind)到窗口的标题

c# - 如何计算一个位置与另一个位置之间的距离/接近度(c#)

C# Textrenderer - 测量较小的字体大小会导致较大的尺寸