在xaml 中引用外部 程序间中图片或样式资源(SVG)
一、创建资源程序集
1 创建一个叫“IconPacks”C#类库程序集
- 删除自动生成的Class1.cs文件
- 添加一个Themes文件夹
- 在Themes文件夹下,新建一个Generic.xaml文件
- 添加一个Images文件夹,以便存放图像相关的资源
- 在Images文件夹下新建一个资源字典文件(例如,命名为IconDictionary.xaml)和一个.CS文件(例如命名为XXXResources.cs)
- 打开AssemblyInfo.cs文件,在文件结尾处添加以下代码,并修正名称空间错误
2、修改XXXResources.cs
文件
在该文件中添加,一些需要使用到的资源索引键,如下所示:
3、修正IconDictionary.xaml
文件
将SvgToXaml.exe
工具生成的xaml文件拷贝到该文件中,并修改每个DrawingImage对象的Key为XXXResources.cs
文件中对应的键。如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:img="clr-namespace:IconPacks.Images" > <DrawingImage x:Key="{x:Static img:IconPacksKeys.ButtonCalibrationKey}"> <DrawingImage.Drawing> <DrawingGroup ClipGeometry="M0,0 V1024 H1024 V0 H0 Z"> <GeometryDrawing Brush="#FF1296DB" Geometry="F1 M1024,1024z M0,0z M374.977267,331.748706C374.977267,347.996762,361.80473,361.168759,345.556008,361.168759L227.871998,361.16876C211.623276,361.16876,198.450739,347.996763,198.450739,331.748707L198.450739,214.069517C198.450739,197.821461,211.623276,184.649464,227.871998,184.649464L345.556009,184.649464C361.804731,184.649464,374.977268,197.821461,374.977268,214.069517L374.977267,331.748706z M600.262241,214.069517C600.262241,197.821461,587.089704,184.649464,570.840982,184.649464L453.156971,184.649464C436.908249,184.649464,423.735712,197.821461,423.735712,214.069517L423.735712,331.748707C423.735712,347.996763,436.908249,361.16876,453.156971,361.16876L570.840982,361.16876C587.089704,361.16876,600.262241,347.996763,600.262241,331.748707L600.262241,214.069517z M824.365247,214.069517C824.365247,197.821461,811.191687,184.649464,794.943988,184.649464L677.258953,184.649464C661.010231,184.649464,647.837694,197.821461,647.837694,214.069517L647.837694,331.748707C647.837694,347.996763,661.010231,361.16876,677.258953,361.16876L794.943988,361.16876C811.191686,361.16876,824.365247,347.996763,824.365247,331.748707L824.365247,214.069517z M375.568763,436.627359C375.568763,420.379303,362.396226,407.207306,346.147504,407.207306L228.463493,407.207305C212.214771,407.207305,199.042234,420.379302,199.042234,436.627358L199.042234,554.307571C199.042234,570.555627,212.214771,583.727624,228.463493,583.727624L346.147504,583.727625C362.396226,583.727625,375.568763,570.555628,375.568763,554.307572L375.568763,436.627359z M600.85476,436.627359C600.85476,420.379303,587.682223,407.207306,571.433501,407.207306L453.748466,407.207305C437.499744,407.207305,424.327207,420.379302,424.327207,436.627358L424.327207,554.307571C424.327207,570.555627,437.499744,583.727624,453.748466,583.727624L571.432477,583.727624C587.681199,583.727624,600.853736,570.555627,600.853736,554.307571L600.853736,436.627359z M375.568763,662.256142C375.568763,646.007063,362.396226,632.836089,346.147504,632.836089L228.463493,632.836089C212.214771,632.836089,199.042234,646.007063,199.042234,662.256142L199.042234,779.935332C199.042234,796.183388,212.214771,809.355385,228.463493,809.355385L346.147504,809.355385C362.396226,809.355385,375.568763,796.183388,375.568763,779.935332L375.568763,662.256142z M600.85476,662.256142C600.85476,646.007063,587.682223,632.836089,571.433501,632.836089L453.748466,632.836089C437.499744,632.836089,424.327207,646.007063,424.327207,662.256142L424.327207,779.935332C424.327207,796.183388,437.499744,809.355385,453.748466,809.355385L571.432477,809.355385C587.681199,809.355385,600.853736,796.183388,600.853736,779.935332L600.853736,662.256142z M824.956743,662.256142C824.956743,646.007063,811.783183,632.836089,795.535484,632.836089L677.850449,632.836089C661.601727,632.836089,648.42919,646.007063,648.42919,662.256142L648.42919,779.935332C648.42919,796.183388,661.601727,809.355385,677.850449,809.355385L795.535484,809.355385C811.783182,809.355385,824.956743,796.183388,824.956743,779.935332L824.956743,662.256142z M824.956743,436.627359C824.956743,420.379303,811.783183,407.207306,795.535484,407.207306L677.850449,407.207306C661.601727,407.207306,648.42919,420.379303,648.42919,436.627359L648.42919,554.307572C648.42919,570.555628,661.601727,583.727625,677.850449,583.727625L795.535484,583.727625C811.783182,583.727625,824.956743,570.555628,824.956743,554.307572L824.956743,436.627359z M786.428709,545.44267L688.003086,545.44267 688.003086,447.02108 786.428709,447.02108 786.428709,545.44267z" /> </DrawingGroup> </DrawingImage.Drawing> </DrawingImage> </ResourceDictionary>
4、修改Generic.xaml
文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,/IconPacks;component/Images/IconDictionary.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
5、编译生成资源程序集
二、使用资源程序集
1、在另一个项目(假设是B项目)中引入 IconPacks 程序集
2、在B项目的App.xaml 中添加引用
<Application x:Class="LYFWPFUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:LYFWPFUI" StartupUri="MVVM\Views\MainView.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--全局--> <ResourceDictionary Source="pack://application:,,/IconPacks;component/Images/IconDictionary.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
3、在要使用的窗体或页面添加名称空间引用
<Page x:Class="LYFWPFUI.MVVM.Views.RatioView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:images="clr-namespace:IconPacks.Images;assembly=IconPacks" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="RatioView">
4、使用资源键
<Button Margin="5"> <Image Source="{StaticResource ResourceKey={x:Static images:IconPacksKeys.ButtonCalibrationKey}}"/> </Button>
5、效果
参考:https://blog.csdn.net/zhy29563/article/details/121360206(这个有-一个步骤没写出来,一个是没有在app.xaml中引用)
标签:文件,SVG,xaml,程序,添加,cs,WPF,资源 From: https://www.cnblogs.com/cdaniu/p/17206763.html