首页 > 编程语言 >【WPF】在xaml 中引用外部 程序间中图片或样式资源(SVG)

【WPF】在xaml 中引用外部 程序间中图片或样式资源(SVG)

时间:2023-03-11 19:34:14浏览次数:49  
标签:文件 SVG xaml 程序 添加 cs WPF 资源

 在xaml 中引用外部 程序间中图片或样式资源(SVG)

一、创建资源程序集

1 创建一个叫“IconPacks”C#类库程序集

 

 

 

  1. 删除自动生成的Class1.cs文件
  2. 添加一个Themes文件夹
  3. 在Themes文件夹下,新建一个Generic.xaml文件
  4. 添加一个Images文件夹,以便存放图像相关的资源
  5. 在Images文件夹下新建一个资源字典文件(例如,命名为IconDictionary.xaml)和一个.CS文件(例如命名为XXXResources.cs)
  6. 打开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

相关文章

  • WPF Validation - Using INotifyDataErrorInfo | .NET Land (kmatyaszek.github.io)
    WPFValidation-UsingINotifyDataErrorInfo|.NETLand(kmatyaszek.github.io)Inthe.NET4.5wasintroducednewinterface INotifyDataErrorInfo whichenable......
  • WPF使用 之一
    关于CheckBox的选中状态(IsChecked)说明,状态最多有三种,因IsChecked属性为bool?是允许为空的,故可以设置第三种状态分别是:已选中IsChecked=true未选中IsChecked......
  • WPF MVVM框架------ Prism中的事件聚合器(IEventAggregator)
    在.net中,事件的订阅和发布使用比较简单,但具有依赖关系。Prism框架中提供的事件聚合器,可实现多点传送发布/订阅功能,可以用于ViewModel与ViewModel、ViewModel与View以及模......
  • HTML5 Canvas 与 SVG 与 div
    动态创建元素并能够移动它们的最佳方法是什么?例如,假设我想创建一个矩形、圆形和多边形,然后选择这些对象并将它们四处移动。我知道HTML5提供了三个元素可以使这成为......
  • 一款WPF开发的B站视频下载开源项目
    一款WPF开发的B站视频下载开源项目2023-02-0908:23·opendotnet欢迎加入 学习小圈子 ,一起打卡交流学习。今天给推荐一款C#开发的、界面简洁的哔哩哔哩视频下载工具......
  • 初识SVG
    解决网站图标问题的最佳方案——SVG!SVG是一种基于XML语法的图像格式,英文全称是:ScalableVectorGraphics,即可缩放矢量图,是W3C的一项建议。我们用手机拍摄的照片,图片格......
  • 一张svg自适应页面H5项目
    做一个自适应的图片,海报,做成简单的自适应网页如下<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <metahttp-equiv="X-UA-Compatible"conten......
  • How can I access the UI element AutoSuggestEdit on wpf in model? I need reload t
    HowcanIaccesstheUIelementAutoSuggestEditonwpfinmodel?IneedreloadtheitemsourceofAutoSuggestEditwheninputtextchangedIntheMVVMpattern,......
  • [WPF]C#连接使用sqllite数据库,,支持多线程操作
    https://www.zhaokeli.com/article/8192.html项目开发环境win764vs2015.net4.6下载  System.Data.SQLite.dll-for-.net4.0.zip  引用到项目中使用sqllite扩展的......
  • WPF 打字效果
    利用 StringAnimationUsingKeyFrames: publicChatGPTWindow(){InitializeComponent();txt1.Text="";//Textblockcontrol......