加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:24:39 所属栏目:百科 来源:网络整理
导读:我正在使用 msdn tutorial在我的Wpf应用程序中添加一个FontFamily,在.csproj我有: ItemGroup Resource Include="ResourcesMetaOT-Norm.otf" / Resource Include="ResourcesMetaOT-Bold.otf" / /ItemGroup 我在ResourceDictionary中添加了fontfamily,就像
我正在使用 msdn tutorial在我的Wpf应用程序中添加一个FontFamily,在.csproj我有:

<ItemGroup>
    <Resource Include="ResourcesMetaOT-Norm.otf" />
    <Resource Include="ResourcesMetaOT-Bold.otf" />
  </ItemGroup>

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

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

但它没有被应用…(我已尝试使用Windows Fonts目录中的Font文件,并且它运行良好).任何的想法 ?

解决方法

如果您使用的是资源字典文件,则必须使用 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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读