相信你們在閱讀WPF相關GitHub開源項目源碼時都會看見一串串這種數據
這種Geometry數據就是幾何圖形數據字體
有一種作法是使用ttf字體文件代替,不過使用ttf字體文件會出現下面幾個缺點:
一、團隊協做不便於管理
二、須要依賴特定平臺
三、沒法靈活使用
而使用Geometry的話,咱們能夠將這些幾何圖形數據存入資源字典ResourceDictionary
經過反射進行靈活使用,團隊開發可共同維護spa
咱們進入https://www.iconfont.cn/官網,找到心儀的圖標,點擊F12將鼠標放在該圖標區域,找到網頁元素
Path標籤內的d屬性即Geometry數據code
建立資源字典,並加入命名空間
將Geometry數據存入
t_Demo即資源名稱key
o:Freeze屬性設置Geometry不可修改,隨後在App.xaml中加入blog
<ResourceDictionary Source="Resources/Themes/BaseStyle.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值get
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源碼