如何使用外部库中的资源(字体、图像、样式)



从外部资源dll加载资源(图像、字体系列、样式(时遇到问题。我希望在一个资源dll中拥有所有资源(图像、字体、样式…(,并将它们设置为可以访问此解决方案中的所有项目(库(。解决方案中的这些项目是由主应用程序引用和调用的不同库。

到目前为止,我尝试了几个不同的命题,但都不起作用。我正在使用Visual studio 2019并为.net 4.6.2进行编译-如果这意味着什么的话。。。

首先,我创建了一个资源库myapp.resources。在这个项目中,我有一个名为Fonts的文件夹,其中是Lato Thin字体。

此外,在该项目的基础上,我创建了一个名为Fonts.xaml 的资源字典

-项目-字体-Lato-Thin.ttf-Fonts.xaml

Fonts.xaml:的结构

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">Fonts/#Lato Thin</FontFamily>    
</ResourceDictionary>

在主应用app.xaml中,我添加了ResourceDictionary的加载:

<ResourceDictionary x:Key="Dict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/myapp.resources;component/Fonts.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

最后一步是将这个字体家族添加到标签控制中:

<Label Grid.Row="0" Grid.Column="0" Content="some random text for test" FontFamily="{StaticResource ResourceKey=LatoThin}"/>

但我总是收到一个错误,即LatoThin字体资源无法定位。

我在任何其他资源类型(如图像、样式(中都会遇到同样的错误。

当然,我添加了对项目的引用,每个文件都在它应该在的地方

唯一有效的方法是,例如,以这种方式将图像添加到按钮中。

<syncfusion:ButtonAdv x:Name="btnSelectFile" 
Grid.Row="1" Grid.Column="2"
VerticalAlignment="Center" SizeMode="Small"
Height="26" Width="26" Label="Button" Margin="3,3,3,3"
SmallIcon="pack://application:,,,/myapp.resources;component/Images/add.png"/>

我试着找一些完整的教程来学习,但我总是遇到同样的错误。

所以,我的问题是:如何解决这个问题?

谢谢你的建议。

确保字体文件(Lato Thin.ttf(的构建操作设置为Resource

在Fonts.xaml中,请确保使用以下格式:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myapp.resources">
<FontFamily x:Key="LatoThin">/myapp.resources;component/Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>

提示:您可以从URI中删除pack://application:,,,以使其更短。

最新更新