c# - 在 resourceDictionary 中添加字体系列

标签 c# .net wpf xaml resources

我正在使用 msdn tutorial在我的 Wpf 应用程序中添加一个 FontFamily,在 .csproj 中我有:

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

我在 ResourceDictionary 中添加了 fontfamily,就像那样:

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

但它没有应用...(我已经尝试使用 Windows 字体目录中的字体文件并且效果很好)。有什么想法吗?

最佳答案

如果您使用的是资源字典文件,则必须使用 Pack URI Scheme 对文件进行寻址。 . 例如:

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

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

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

举个例子:

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!--A resource dictionary in the output folder in the Assets folder-->
            <ResourceDictionary Source="pack://siteoforigin:,,,/Assets/OpenIconsDictionary.xaml"/>
            <!--A resource dictionary packed in the Gui dll-->
            <ResourceDictionary Source="pack://application:,,,/Gui;component/Assets/PackedIconsDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
</UserControl.Resources>



<!--In the output folder /Assets/OpenIconsDictionary.xaml (Build Action: Embedded Resource, Copy always)-->
<ResourceDictionary
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <BitmapImage x:Key="Icon"                 
                 UriSource="pack://siteoforigin:,,,/Images/image.png"/>


</ResourceDictionary>


<!--In Gui.dll in the folder /Assets/PackedIconsDictionary.xaml (Build Action: Page, Do not copy)-->
<ResourceDictionary
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <BitmapImage x:Key="Icon"                 
                 UriSource="pack://siteoforigin:,,,/Images/image.png"/>


</ResourceDictionary>

关于c# - 在 resourceDictionary 中添加字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706489/

相关文章:

c# - 如何将对象更改保存到其 XAML?

c# - 如何动态附加到 WinRT 事件?

c# - 在特定时间部署版本

.net - ASP.net MVC 中的 session 结束

.net - 如何使用连接 PLINQ 现有的 LINQ 查询?

c# - 在不创建新的 Edmx 文件的情况下在 EF 4 上映射外键

c# - startinfo.arguments 问题?

c# - C#中的Click事件和用户定义的事件有什么区别

c# - 集合属性应该是只读的

c# - 如何在 PDFSharp 中使用私有(private)字体