Font embedding allows you to place a third-party font into your application. Numerous websites offer fonts for purchase, and some even offer free fonts.
Note
Like all media sourced from external sources, be sure that you are legally entitled to redistribute fonts that have been embedded in your application. Be aware that the licensing restrictions of some fonts allow you to create graphics with the font but not to redistribute the font itself.
To embed a font, include it in your Visual Studio project. Set the Build Action in the Visual Studio Properties pane to Content (see Figure 6.5). This causes the font file to be placed in the XAP file when the project is built.
The difference between the Content build action and the Resource build action is that the Resource option causes files to be placed in the project assembly, whereas Content files are not placed into an assembly but rather sit independently from the assembly inside the XAP file. This is important for performance reasons, because minimizing the assembly size causes it to load faster and ultimately improves the startup time of your app.
Embedding the font allows you to set the FontFamily
of text controls by providing the path to the font file, as shown in the following example:
<TextBlock Text="Embedded Font"
FontFamily="/ControlExamples/Fonts/orbitron-medium.ttf#Orbitron" />
Notice that the format for the path ends in a hash followed with the name of the font. This value allows the runtime environment to find the font within the font file, because some font files may contain more than one font. To determine the name of the font in Windows 7, double-click the font to open the Windows Font Preview tool (see Figure 6.6). For other operating systems, you might need to download a third-party font viewer.
Defining the FontFamily
for each control used within your project is not a sound practice, because if the font is moved, or you later decide to use a different font, it will require updating the property across your project. Instead, define the FontFamily
in the application’s Resources and include a Style
for TextBlocks
that will use the FontFamily
like so:
<Application.Resources>
<FontFamily x:Key="EmbeddedFont">
/ControlExamples/Fonts/orbitron-medium.ttf#Orbitron
</FontFamily>
<Style x:Key="EmbeddedTextStyle" TargetType="TextBlock"
BasedOn="{StaticResource PhoneTextNormalStyle}">
<Setter Property="FontFamily"
Value="{StaticResource EmbeddedFont}" />
</Style>
</Application.Resources>
It can then be used throughout your application in the following manner:
<TextBlock Text="Embedded font defined in app Resources
Style="{StaticResource EmbeddedTextStyle}"/>
Note
The license terms of the Segoe WP fonts do not allow redistribution. This means packaging them within your application’s XAP file is not allowed. Of course, you can still use the font in your app without embedding it.
3.14.129.194