第10章 资源(3)——程序集间共享资源

2024-01-22 06:18

本文主要是介绍第10章 资源(3)——程序集间共享资源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、概述

当共享包含一个或多个资源字典的编译过的程序集时,有三种方法提取所希望的资源并在应用程序中使用。

三种方法的前提是在应用程序中添加对共享程序集的引用

二、C#代码方式

只要知道资源所在xaml文件和资源名即可

ResourceDictionary resdic = new ResourceDictionary();
resdic.Source = new Uri("/MyContorlLib;component/MyImageBrush.xaml", UriKind.Relative);
btn_Share.Background = (ImageBrush)resdic["haha"];

三、URI定位方式


完整代码如下:

MyImageBrush.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:ResourceLibrary"><ImageBrush x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"ImageSource="/ResourceLibrary;component/Images/smile.png" Opacity="0.3"></ImageBrush>
</ResourceDictionary>
调用程序:App.xaml
<Application x:Class="ResourceTest.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:ResourceTest"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/ResourceLibrary;component/MyImageBrush.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>
调用程序:MainWindow.xaml
<Window x:Class="ResourceTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:ResourceTest"mc:Ignorable="d"Title="MainWindow" Height="350" Width="525"><StackPanel><Button x:Name="btn_Share" Background="{DynamicResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">SharedResource</Button></StackPanel>
</Window>

四、ComponentResourceKey标记扩展方式,固定写法,以后按照后面对应的步骤编写即可。

共享资源库的创建不能选择类库,应选择自定义控件库,创建项目的同时会自动创建Theme文件夹和Generic.xaml,将自动生成的CustomControl1类删除即可。


步骤如下:

对共享资源字典进行如下操作时有可能遇到Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'MyControlLib' that is not included in the assembly.也就是“未定义的CLR名称空间,......”类的错误,这时可通过【重新生成】共享资源项目即可。

①在共享资源项目上创建Themes文件夹

②在Themes文件夹里面创建Generic.xaml资源字典


③将共享资源添加到Generic.xaml

<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:MyContorlLib"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="MyImageBrush.xaml"/></ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

④创建个类用于在调用程序中引用共享资源

⑤给创建的类添加ComponentResourceKey静态属性,便于调用程序的引用,格式如下:

public static ComponentResourceKey haha
{get{return new ComponentResourceKey(typeof(CustomResource), "haha");}
}

⑥重写共享资源的x:Key属性,格式如下:

x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResource}, ResourceId=haha}"
⑦调用程序添加对共享资源的引用,格式如下:

xmlns:res="clr-namespace:MyContorlLib;assembly=MyContorlLib"
⑧使用static标记扩展实现对共享资源的使用
Background="{DynamicResource {x:Static res:CustomResource.haha}}"
完整代码如下:

MyImageBrush.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:MyContorlLib"><ImageBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResource}, ResourceId=haha}"TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"ImageSource="/MyContorlLib;component/Images/smile.png" Opacity="0.3"></ImageBrush>
</ResourceDictionary>
自定义类:CustomResource.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace MyContorlLib
{
public class CustomResource
{public static ComponentResourceKey haha{get{return new ComponentResourceKey(typeof(CustomResource), "haha");}}
}
}
Generic.xaml
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:MyContorlLib"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="MyImageBrush.xaml"/></ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
应用程序:MainWindow.xaml
<Window x:Class="ResourceTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:ResourceTest"xmlns:res="clr-namespace:MyContorlLib;assembly=MyContorlLib"mc:Ignorable="d"Title="MainWindow" Height="350" Width="525"><StackPanel><Button x:Name="btn_Share" Background="{DynamicResource {x:Static res:CustomResource.haha}}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">SharedResource</Button></StackPanel>
</Window>

这篇关于第10章 资源(3)——程序集间共享资源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/632089

相关文章

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Java使用WebView实现桌面程序的技术指南

《Java使用WebView实现桌面程序的技术指南》在现代软件开发中,许多应用需要在桌面程序中嵌入Web页面,例如,你可能需要在Java桌面应用中嵌入一部分Web前端,或者加载一个HTML5界面以增强... 目录1、简述2、WebView 特点3、搭建 WebView 示例3.1 添加 JavaFX 依赖3

防止SpringBoot程序崩溃的几种方式汇总

《防止SpringBoot程序崩溃的几种方式汇总》本文总结了8种防止SpringBoot程序崩溃的方法,包括全局异常处理、try-catch、断路器、资源限制、监控、优雅停机、健康检查和数据库连接池配... 目录1. 全局异常处理2. 使用 try-catch 捕获异常3. 使用断路器4. 设置最大内存和线