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

发布于 2024-12-23 15:08:12 字数 1070 浏览 1 评论 0原文

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

I'm having problems in rendering custom textblock with custom font resource on liveTile ?

My project updates a live tile in background. but it should be personalized.

Im using this code. but its does not woks, the text shows blank when i try to use an embedded font
the bitmap background works just fine. But the fonts does not works.

When i use this same code in "foreground agent" the fonts shows perfectly.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-12-30 15:08:12

我遇到了类似的问题,并找到了解决方案,请查看此处或<一个href="http://social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/abbbf3c1-7970-46f9-9560-661965ae33ed/" rel="nofollow noreferrer">这里

我沸腾了降至任一
1. 在进行转换之前创建一个使用该字体的隐藏文本块,或者
2.创建一个FontSource。

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

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

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

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

I had a similar problem, and found a solution, please have a look here or here

I boils down to either
1. creating a hidden textblock that use the font before doing the conversion, or
2. creating a FontSource.

I used the first because it was simpler for now.

The textblock is hidden, but it ensures that the embedded font is loaded.

Inside my control, I added the following:

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
嗼ふ静 2024-12-30 15:08:12

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

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

I wouldn't be certain the issue is related to the posted code. I find it likely that loading a custom font, will take quite some time, and thus the Background Agent will finish before the font is loaded, and as such not rendering anything.

Are you certain the rendering completes, before you call NotifyComplete() ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文