2011-12-31 40 views
2

我在使用liveTile自定義字體資源渲染自定義文本塊時遇到問題?Dynamic LiveTile - 在後臺任務上渲染LiveTile上的自定義字體TextBlock

我的項目在後臺更新活動圖塊。但它應該是個性化的。

使用此密碼。但它沒有炒作,當我嘗試使用嵌入的字體時,文本顯示爲空白 位圖背景工作得很好。但字體不起作用。

當我在「前臺代理」中使用相同的代碼時,字體完美顯示。

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

回答

1

我不確定這個問題與發佈的代碼有關。我發現加載自定義字體可能需要相當長的時間,因此後臺代理將在加載字體之前完成,因此不會呈現任何內容。

在您致電NotifyComplete()之前,您確定渲染完成嗎?

+0

我明白了...但是,我怎麼能肯定的是,在字體加載完全地?有回調嗎? – 2012-01-02 23:14:25

+0

沒有。所以你必須猜測。你可以嘗試一些人工線程塊幾秒鐘,看看是否有幫助。 – 2012-01-02 23:23:26

1

我也有類似的問題,並找到了解決辦法,請看看herehere

我歸結爲兩種 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