第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

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

Rust中的Drop特性之解读自动化资源清理的魔法

《Rust中的Drop特性之解读自动化资源清理的魔法》Rust通过Drop特性实现了自动清理机制,确保资源在对象超出作用域时自动释放,避免了手动管理资源时可能出现的内存泄漏或双重释放问题,智能指针如B... 目录自动清理机制:Rust 的析构函数提前释放资源:std::mem::drop android的妙

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

EMLOG程序单页友链和标签增加美化

单页友联效果图: 标签页面效果图: 源码介绍 EMLOG单页友情链接和TAG标签,友链单页文件代码main{width: 58%;是设置宽度 自己把设置成与您的网站宽度一样,如果自适应就填写100%,TAG文件不用修改 安装方法:把Links.php和tag.php上传到网站根目录即可,访问 域名/Links.php、域名/tag.php 所有模板适用,代码就不粘贴出来,已经打