wpf多程序集之间共享资源字典--CLR名称空间未定义云云
以下介绍如何创建可用于在多个程序集之间共享的资源字典
1.新建Wpf自定义控件库,名称为MyContorlLib,项目资源结构图如下:
2.打开Themes文件夹中的Generic.xaml文件,在根元素中添加如下名称空间:
xmlns:local="clr-namespace:MyContorlLib"
因为待会儿要用到该名称空间下的一个类CustomControl1(系统自动生成的,也可以自己创建)
3.在资源字典中添加资源:
- <LinearGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomControl1}, ResourceId=haha}" StartPoint="0,0" EndPoint="0,1">
- <GradientStop Color="Blue" Offset="0"></GradientStop>
- <GradientStop Color="White" Offset="0.5"></GradientStop>
- <GradientStop Color="Red" Offset="0.7"></GradientStop>
- <GradientStop Color="Firebrick" Offset="1"></GradientStop>
- </LinearGradientBrush>
<LinearGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomControl1}, ResourceId=haha}" StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Blue" Offset="0"></GradientStop> <GradientStop Color="White" Offset="0.5"></GradientStop> <GradientStop Color="Red" Offset="0.7"></GradientStop> <GradientStop Color="Firebrick" Offset="1"></GradientStop> </LinearGradientBrush>
重点在这里,注意看x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomControl1}, ResourceId=haha}",可以发现它跟我们在Window.Resources或Application.Resources或其他元素中定义的x:Key的方式略有区别;
解释一下:
x:Key不再是字符串,而是一个更加复杂的类型ComponentResourceKey,它有三个属性TypeInTargetAssembly、ResourceId、Assembly,我们这里用到了前面两个;
TypeInTargetAssembly属性需要接收一个Type类型的值,我们赋予{x:Type local:CustomControl1};
ResourceId需要接收一个字符串,名字随便取,这里赋予haha(没有什么含义,有含义最好)
这样就为资源元素定义好了Key,下面就是在另一个程序集中应用它了.
略述步骤如下:
1.创建WPF项目,名称为WpfAPP;
2.创建一个Window窗口,名称为resTest;
3.添加程序集引用,具体是右击"引用"-->"添加"-->"项目"-->"MyControlLib"-->"确定"
4.打开resTest.xaml文件,向根元素添加名称空间:
xmlns:res="clr-namespace:MyContorlLib;assembly=MyControlLib"
注意与上面的写法略有不同(具体的原因我也不好解释,好像上面的名称空间也可以写成这样)
5.添加一个Label元素,为其Background属性赋值如下:
Background="{DynamicResource ResourceKey={ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomControl1}, ResourceId=haha}}"
这里我们用到了原先为资源元素定义好的Key,是个ComponentResourceKey,包含两个必要的属性.
6.工作完成.
还有一步额外但是很有趣的操作:
1.打开CustomControl1.cs文件;
2.添加一个静态公开的ComponentResourceKey类型的属性:
- public static ComponentResourceKey LinearGradientColorBrushKey {
- get { return new ComponentResourceKey(typeof(CustomControl1),"haha"); }
- }
public static ComponentResourceKey LinearGradientColorBrushKey { get { return new ComponentResourceKey(typeof(CustomControl1),"haha"); } }
3.这样我们也可以获得资源,我们重新为Label的Background属性赋值:
Background="{DynamicResource ResourceKey={x:Static Member=res:CustomControl1.LinearGradientColorBrushKey}}"
4.也有相同的效果.
下面来解决在这个过程中可能遇到的问题:
Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'MyControlLib' that is not included in the assembly.
也就是“未定义的CLR名称空间,......”类的错误
其实如果按照上面说的做逻辑上没有错误,但是VS或Blend总要提醒本错误,原因就是:
我们没有“重新构建”或“重新生成”MyControlLib程序集,重新生成一下试试吧,会成功的。
至于为何要“重新生成”,我想用不着我多解释把吧,自己想想也就明白了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器