2010-02-04 60 views
1

我在Silverlight 3的外部庫中使用合併字典,並且我在資源文件(styles.xaml)中定義的樣式正確應用於我的按鈕控件。未找到合併字典的字體

我想將字體應用於我的按鈕,並將字體放在與我的styles.xaml相同的目錄中(在構建操作的外部dll中,作爲資源)。在styles.xaml我有:

<Style x:Key="MyButtonStyle" 
     TargetType="Button"> 
    <Setter Property="Background" 
      Value="#FF1F3B53" /> 
    <Setter Property="Foreground" 
      Value="#FF000000" /> 
    <Setter Property="Padding" 
      Value="3" /> 
    <Setter Property="BorderThickness" 
      Value="1" /> 
    <Setter Property="FontFamily" 
      Value="VINERTIC.TTF#Viner Hand ITC" /> 

等。然而,不施加

的字體。我曾嘗試將字體文件放在App.XAML目錄中,但仍未應用。 如果我在樣式外應用字體,那麼它工作正常。

JD

回答

1

好吧,我想我現在已經明白了。事實證明,您需要引用您的字體文件和它所在的程序集的路徑。想象一下,您有一個名爲MyResourceAssembly的單獨程序集,其中包含名爲Resources的文件夾。在這個文件夾中有Assets1.xaml,Assets2.xaml和你的字體文件。所有三個構建操作都設置爲「資源」。在您的應用程序中(讓我們稱之爲MyApp),您可以在App.xaml中合併兩個資源文件。 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> 

這就是你如何在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> 
+0

@亨利克。謝謝你的詳細回覆。我會在今天晚些時候發佈它,並告訴你它是否全部有效。 – 2010-02-09 12:45:07

+0

謝謝你的解釋。 – 2010-02-11 18:31:53

0

編輯
忽略這個答覆,如果一切都在同一個裝配它纔會起作用。


我只是嘗試這樣做, 和它的作品對我來說 。這是我做的方式:

字體文件(ttf)位於應用程序的根目錄。構建操作是「資源」,選擇「不復制」。

我有一個「資源」文件夾,也在應用程序的根目錄。在這我有Assets1.xaml和Assets2.xaml。構建操作都是「資源」,並且「不要複製」被選中。在Assets1.xaml中,我有一些無關緊要的東西。在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="ProggyTiny.ttf#ProggyTinyTT"></Setter> 

     <Setter Property="FontSize" 
       Value="16"></Setter> 

    </Style> 

</ResourceDictionary> 

在App.xaml中我做到這一點(請注意,我用的是基類我的應用程序,但這不應該有所作爲):

<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> 

當我將樣式應用於同一項目中的文本框時,它會使用自定義字體顯示。

+0

對不起我過早的答覆。當你在單獨的程序集中處理東西時顯然更復雜。但我認爲我已經解決了,我只需要稍微處理一下。我會回來的... – 2010-02-05 08:31:48