Font not found with merge dictionary
I am using merged dictionaries in an external library with Silverlight 3 and the style I have defined in the resource file (styles.xaml) is applied correctly to my button controls.
I want to apply a font to my buttons and I have the font in the same directory as my styles.xaml (in the external dll with build action as resource). In the styles.xaml I have:
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="#FF1F3开发者_Python百科B53" />
<Setter Property="Foreground"
Value="#FF000000" />
<Setter Property="Padding"
Value="3" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="FontFamily"
Value="VINERTIC.TTF#Viner Hand ITC" />
etc.
However, the font is not applied. I have tried putting the font file in the App.XAML directory and still it is not applied. If I apply the font outside the style, then it works fine.
JD
Ok, I think I've got it now. It turns out that you need to reference your font file with the path to the assembly it's in. Imagine you have a separate assembly called MyResourceAssembly with a folder called Resources. In this folder are Assets1.xaml, Assets2.xaml and your font file. Build action is set to "Resource" for all three. In your application (let's call it MyApp) you have your App.xaml where you merge the two resource files.
Content of Assets2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="DeveloperStyle"
TargetType="TextBox">
<Setter Property="FontFamily"
Value="/MyResourceAssembly;component/Resources/ProggyTiny.ttf#ProggyTinyTT"></Setter>
<Setter Property="FontSize"
Value="16"></Setter>
</Style>
</ResourceDictionary>
And this is how you merge the resource dictionaries in App.xaml:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyApp">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyResourceAssembly;component/Resources/Assets1.xaml" />
<ResourceDictionary Source="/MyResourceAssembly;component/Resources/Assets2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
EDIT
Ignore this reply, it only works if everything is in the same assembly.
I just tried this, and it works for me. This is the way I did it:
The font file (ttf) is at the root of the app. Build action is "Resource" and "Do not copy" is selected.
I have a "Resources" folder, also at the root of the app. In this I have Assets1.xaml and Assets2.xaml. Build action for both are "Resource", and "Do not copy" is selected. In Assets1.xaml I have some stuff that does not matter. In Assets2.xaml I put the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="DeveloperStyle"
TargetType="TextBox">
<Setter Property="FontFamily"
Value="ProggyTiny.ttf#ProggyTinyTT"></Setter>
<Setter Property="FontSize"
Value="16"></Setter>
</Style>
</ResourceDictionary>
In App.xaml I do this (note that I use a base class for my app, but that shouldn't make a difference):
<base:BaseApplication xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:base="clr-namespace:GLS.Gui.Controls.Base;assembly=GLS.Gui.Controls"
x:Class="GLSTestApp02.App"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:h="clr-namespace:GLS.Gui.Helper;assembly=GLS.Gui.Helper">
<base:BaseApplication.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Assets1.xaml" />
<ResourceDictionary Source="Resources/Assets2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</base:BaseApplication.Resources>
</base:BaseApplication>
When I apply the style to a TextBox in the same project, it is displayed using the custom font.
精彩评论