Leveraging FontSource to Assign a Font Using a Stream

,

While font embedding is restricted to fonts located within your app’s XAP file, the FontSource property of the TextBlock, TextBox, and PasswordBox allows you to download the font using an external data source, such as a cloud service.

The FontSource class requires a Stream of font data. The following example takes the same embedded font used in the previous example and provides it to a TextBlock object’s FontSource property via a Stream, using the Application.GetResourceStream method as shown:

Uri fontUri = new Uri("ControlExamples/Fonts/orbitron-medium.ttf",
                                                    UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(fontUri);
textBlockWithFontSource.FontSource = new FontSource(resourceInfo.Stream);
textBlockWithFontSource.FontFamily = new FontFamily("Orbitron");

The origin of the font file is arbitrary and could have just as easily been supplied via a web service.

Although this approach offers a great deal of flexibility, ordinarily it would be useful only for a highly specialized, perhaps font-related, app. If the font can be determined at development time, it is best to use embedding rather than the FontSource property to decrease the dependency on network services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.81.214