C# WPF如何反射加载Geometry几何图形数据图标
作者:Stay627 时间:2021-09-06 11:39:08
目录
为什么要用Geometry数据做图标?
怎么获取Geometry数据?
如何使用Geometry数据
相信大家在阅读WPF相关GitHub开源项目源码时都会看见一串串这种数据
这种Geometry数据就是几何图形数据
为什么要用Geometry数据做图标?
有一种做法是使用ttf字体文件代替,不过使用ttf字体文件会出现下面几个缺点:
1、团队协作不便于管理
2、需要依赖特定平台
3、无法灵活使用
而使用Geometry的话,我们可以将这些几何图形数据存入资源字典ResourceDictionary
通过反射进行灵活使用,团队开发可共同维护
怎么获取Geometry数据?
我们进入https://www.iconfont.cn/官网,找到心仪的图标,点击F12将鼠标放在该图标区域,找到网页元素
Path标签内的d属性即Geometry数据
如何使用Geometry数据
创建资源字典,并加入命名空间
将Geometry数据存入< Geometry x:Key="t_chart" o:Freeze="true" >< /Geometry >标签内
t_chart即资源名称key
可能会有小伙伴注意到了o:Freeze这个属性,下面是MSDN上的原文
A class that derives from Freezable gains the following features:
Special states: a read-only (frozen) state and a writable state.
Thread safety: a frozen Freezable object can be shared across threads.
Detailed change notification: Unlike other DependencyObject objects, a Freezable object provides change notifications when sub-property values change.
Easy cloning: the Freezable class has already implemented several methods that produce deep clones.
翻译后:
从Freezable派生的类具有以下功能:
特殊状态:只读(冻结)状态和可写状态。
线程安全:冻结的Freezable对象可以在线程之间共享。
详细的更改通知:与其他DependencyObject对象不同,Freezable对象在子属性值更改时提供更改通知。
易于克隆:Freezable类已经实现了几种产生深层克隆的方法。
随后在App.xaml中加入
<ResourceDictionary Source="Resources/Themes/Geometries.xaml" />
这样我们就可以在全局的XAML代码中通过{StaticResource t_Demo}使用Geometry数据
那么肯定会有小伙伴问了,如果想使用MVVM前后台分离开发怎么办?(在C#代码中动态使用Geometry)
下面是反射加载Geometry的示例
将资源文件存入静态类中
namespace Demo.Resources.Themes
{
public static class LocalTheme
{
public static ResourceDictionary Dic = new ResourceDictionary { Source = new Uri(@"Resources/Themes/Geometries.xaml", UriKind.Relative) };
}
}
使用资源字典(Geometry)LocalTheme.Dic["t_chart"],t_chart即资源字典中的key值
var chart = new HandyControl.Controls.TabItem()
{
Header="图表",
Content = xamlModel
};
chart.SetValue(IconElement.GeometryProperty, (Geometry)LocalTheme.Dic["t_chart"]);
SetValue即设置附加属性
public void SetValue(DependencyProperty dp, object value);
中的value为Geometry
来源:https://www.cnblogs.com/Stay627/archive/2021/03/26/14583581.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法
![](https://img.aspxhome.com/file/2023/6/74816_0s.jpg)
Java上传文件进度条的实现方法(附demo源码下载)
SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例
浅谈Java生命周期管理机制
Java实现滑动验证码的示例代码
![](https://img.aspxhome.com/file/2023/0/74890_0s.jpg)
JAVA简单工厂模式(从现实生活角度理解代码原理)
java快速生成数据库文档详情
![](https://img.aspxhome.com/file/2023/3/59363_0s.png)
Springboot通过谷歌Kaptcha 组件生成图形验证码功能
![](https://img.aspxhome.com/file/2023/9/69659_0s.png)
java获取网络图片上传到OSS的方法
javaWeb使用servlet搭建服务器入门
@Autowired注解注入的xxxMapper报错问题及解决
![](https://img.aspxhome.com/file/2023/5/63635_0s.jpg)
java身份证验证代码实现
解决mybatis update并非所有字段需要更新问题
![](https://img.aspxhome.com/file/2023/9/61599_0s.jpg)
IDEA不编译除了.java之外的文件的解决办法(推荐)
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
Java多线程Thread基础学习
![](https://img.aspxhome.com/file/2023/8/67688_0s.png)
Java AtomicInteger类使用方法实例讲解
C# Dynamic之:ExpandoObject,DynamicObject,DynamicMetaOb的应用(上)
![](https://img.aspxhome.com/file/2023/1/74811_0s.jpg)
Eclipse的Debug调试技巧大全(总结)
![](https://img.aspxhome.com/file/2023/3/59993_0s.png)
Spring Boot Logback配置日志过程解析
Java如何优雅替换if-else语句
![](https://img.aspxhome.com/file/2023/8/71868_0s.jpg)