c# - 自定义字体在 Uno.WASM 中有效,但在 Uno.UWP 中无效
问题描述
由于搜索了一段时间后我没有看到有用的东西,所以我决定在这里问:
我有一个小型测试项目,只是为了从互联网导入自定义图标字体。当前程序如下:我的 MainApp.xaml 由一个文本块组成,它引用 Styles.xaml 中的样式元素,该元素又引用了一个 FontFamily,我在每个平台上加载 ttf 文件。
在 WASM 中效果很好(base64 URI),但在 UWP 中我根本无法显示字体图标。Style.xaml 由 App.xaml 完美导入,Size 被应用,但 FontFamily-Tags 似乎有问题。
我试过:
- 手动安装我的字体 => 工作就像一个魅力。所以它可能不是字体文件?
- https://platform.uno/docs/articles/features/custom-fonts.html,但没有帮助。
- https://blog.mzikmund.com/2020/01/custom-fonts-in-uno-platform/这反过来并没有太大变化。
- https://github.com/MartinZikmund/blog-2020/tree/master/Uno.CustomFonts即使在这里交叉引用之后,我也无法让它工作。
- https://github.com/unoplatform/calculator/blob/uno/src/Calculator.Shared/Styles.xaml我还与官方的计算器端口进行了交叉检查,但即使像他们那样进行设置后,也没有任何改变。
- 重新安装 VS(我使用社区版)。这是我的代码:
MainApp.xaml:
<Page
x:Class="Testing.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="L" Style="{ThemeResource IconTextStyle}" />
</Grid>
</Page>
样式.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:xamarin="http://uno.ui/xamarin"
xmlns:macos="http://uno.ui/macos"
xmlns:wasm="http://uno.ui/wasm"
xmlns:skia="http://uno.ui/skia"
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="xamarin wasm macos skia"
>
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</macos:FontFamily>
<wasm:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</wasm:FontFamily>
<skia:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</skia:FontFamily>
<win:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</win:FontFamily>
<macos:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</macos:FontFamily>
<wasm:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</wasm:FontFamily>
<skia:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</skia:FontFamily>
<Style TargetType="TextBlock" x:Key="IconTextStyle">
<Setter Property="FontFamily"
Value="{StaticResource IconScratchedFontFamily}" />
<Setter Property="FontWeight"
Value="Normal" />
<Setter Property="FontSize"
Value="116" />
</Style>
</ResourceDictionary>
应用程序.xaml:
<Application
x:Class="Testing.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
解决方案
找到了解决方案:
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</macos:FontFamily>
背景:#后面的部分表示字体的名称,因为它写在 .ttf 文件中,而不是我要引用的字体名称,如果您考虑一下,这很明显。
用我提供的代码几乎不可能弄清楚,所以我决定为遇到这个问题的其他人更新我的问题。
推荐阅读
- webpack - fsevents 导致模块解析失败:意外字符“�”
- python - 如何使用散景从回调中更新轴标签?
- java - Java中可重入锁中的同步(对象)等价物是什么?
- typescript - 如何在 type-graphql 中触发订阅?
- javascript - 无法读取未定义的属性,即使我已经对其进行了初始化。(角度)
- r - 单击后将 shinydashboardPlus 侧边栏中的每个选项卡与主体中的不同选项卡连接起来
- c# - 字典
到 npgsqlParmeterCollection? - python - 如果未建立 Web 套接字连接,如何发送消息或中止到 http 错误页面?
- swift - 应用程序因更改音量而崩溃,状态 KVO 消息已收到但未针对 keypath “outputVolume”进行处理
- angular - 错误:没有用于具有未指定名称属性的表单控件的值访问器 - 角度反应式表单单元测试错误