添加字体系列中的ResourceDictionary字体、系列、ResourceDictionary

2023-09-05 23:25:23 作者:笑着原谅

我使用的是 MSDN教程添加的FontFamily在我的WPF应用程序中的.csproj我有:

 < ItemGroup>
    <资源包含=资源\ MetaOT-Norm.otf/>
    <资源包含=资源\ MetaOT-Bold.otf/>
  < / ItemGroup>
 

我添加了fontFamily中的ResourceDictionary中,这样的:

 <的FontFamily X:关键=FontMetaOT> ./资源/#元OT< /的FontFamily>
 

但它并不适用。(我曾尝试与在windows字体目录下的字体文件,它是不错的选择)。你知道吗?

解决方案 C WPF XAML事件 资源 样式

如果您使用的是资源字典文件,可以选择使用的包URI方案。 例如:

  

下面的例子显示了包URI的XAML资源文件   位于引用的程序集的项目文件夹的根目录下。

 包://应用:,,, / ReferencedAssembly;组件/ ResourceFile.xaml
 

  

下面的例子显示了包URI的XAML资源文件   位于引用的程序集的项目文件夹的子文件夹。

 包://应用:,,, / ReferencedAssembly;组件/子文件夹/ ResourceFile.xaml
 

  

下面的例子显示了包URI的XAML资源文件   位于引用,版本特定的根文件夹   装配的项目文件夹。

 包://应用:,,, / ReferencedAssembly; V1.0.0.1;组件/ ResourceFile.xaml
 

如果该文件位于输出文件夹,您可以使用源站点引用它:

  

下面的例子显示了包的URI来源的XAML网站   文件中,存储在从该可执行组件的位置   推出。

 包:// siteoforigin:,,, / SiteOfOriginFile.xaml
 

  

下面的例子显示了包的URI来源的XAML网站   文件中,存储在子文件夹,是相对于的位置从中   该应用程序的可执行组件启动。

 包:// siteoforigin:,,, /子文件夹/ SiteOfOriginFile.xaml
 

I am using the msdn tutorial to add a FontFamily in my Wpf application, in .csproj I have :

  <ItemGroup>
    <Resource Include="Resources\MetaOT-Norm.otf" />
    <Resource Include="Resources\MetaOT-Bold.otf" />
  </ItemGroup>

I added the fontfamily in the ResourceDictionary, like that :

<FontFamily x:Key="FontMetaOT">./Resources/#Meta OT</FontFamily>

But it's not applied... (I have tried with the Font files in the windows Fonts directory and it's work well). Any idea ?

解决方案

If you are using a resource dictionary file, you have to address the files using the Pack URI Scheme. For instance:

The following example shows the pack URI for a XAML resource file that is located in the root of the referenced assembly's project folder.

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.

pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder.

pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

If the file is located in the output folder, you can use site of origin to refer to it:

The following example shows the pack URI for a XAML site of origin file, stored in the location from which the executable assembly is launched.

pack://siteoforigin:,,,/SiteOfOriginFile.xaml

The following example shows the pack URI for a XAML site of origin file, stored in subfolder that is relative to the location from which the application's executable assembly is launched.

pack://siteoforigin:,,,/Subfolder/SiteOfOriginFile.xaml