Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App-wide Font resources in Silverlight

I'm looking to see if it is possible to use app-wide font in Silverlight controls that can be set in a application.resources and then used throughout the app. They don't need to specifify the other things about a font like bold or italic, just the name.

Mostly, this is for custom fonts that would require to be embedded into the app. For example, I'm looking for something simple like (not of any particular control or control type)...

<Setter x:Key="My First Font"
        Property="FontFamily"
        Value="VINERTIC.TTF#Viner Hand ITC" />

Then in any given control I would type...

<TextBlock FontFamily="{StaticResource "My First Font"}"
           x:Name="ApplicationTitle"
           Text="NEXT PAGE" Foreground="Red"/>

...or...

FontFamily="{Binding "My First Font"}"

or some such thing.

I know what I did doesn't work, but that is the desired effect.

I can't seem to find any documention on how to set app-wide font families. Any advice?

like image 394
Stan Avatar asked Oct 14 '22 20:10

Stan


1 Answers

Here is some information I think will get you started.

First off, the easiest way I know to embed a font in a shared library (ex: a Silverlight Class library), is to do the following:

  1. Have a class library, and know its default namespace. For this example, the default namespace of the class library is Common.Windows.
  2. Have a font. For this example, Action Man Bold.ttf, which contains the named font Action Man.
  3. Add the font somewhere in the shared library project. For example, the Assets/Fonts folder.
  4. Set the build action on the file to Resource.
  5. Build the project.

After following these steps, you can refer to the font wherever you please, including styles, with syntax similar to the text block below:

<TextBlock
 FontFamily="/Common.Windows;component/Assets/Fonts/Action Man Bold.ttf#Action Man" />

Note that the FontFamily expression above worked within the container project and within a referencing project. I didn't test this process with a zip archive of fonts, but I'd bet a fancy rock out of the garden that it would still work.

That should get you started. If you are interested in going the extra (and useful) step of declaring the font in a style, and using the style on your controls, consider taking a look at this resource:

http://www.silverlight.net/learn/quickstarts/control-styles/

like image 96
kbrimington Avatar answered Oct 18 '22 01:10

kbrimington