c# - 从 Wpf 中的资源中的 CodeBehind 设置 FontFamily
问题描述
嗨,我想从 codebehind 更改 fontfamily,但我的字体是一种资源样式,我该怎么做?
这是我的字体字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FontFamily x:Key="IRANSans">IRANSans, pack://application:,,,/PersianCalendar;component/Resources/Fonts/Fonts/#IRANSans</FontFamily>
</ResourceDictionary>
在 xaml 我可以这样设置字体:
FontFamily="{StaticResources IranSans}"
但我不知道如何在代码隐藏中做到这一点,我想要这样的东西
pt.FontFamily = new FontFamily(TryFindResources("IranSans"));
解决方案
您可以使用var fontFamily = this.Resources["IRANSans"];
推荐阅读
- ballerina - Ballerina 数组操作(如 push 和 pop)是线程安全的吗?
- arm - 保存 Cortex-M4 处理器状态以进行断电和恢复
- javascript - 使用 js SDK 向 Amazon Lex 发送额外的详细信息
- excel - 如何使用 Excel VBA 设置一个变量以过滤不同的文本字符串值?
- tensorflow - 如何在张量流中生成 VGG16 多特征
- wordpress - Wordpress:自动在帖子中添加来自 youtube 视频的图像
- python - 为什么我们不能在 UI 线程之外更新 UI 小部件?
- swift - 使用 Alamofire POST 和解析响应 | 斯威夫特 5
- python - 我对现有代码有一些问题。我正在使用 Facenet 进行人脸识别
- swift - 如何使用 swiftui 创建表情符号键盘?