zoukankan      html  css  js  c++  java
  • wpf多程序集之间共享资源字典--CLR名称空间未定义云云

    wpf多程序集之间共享资源字典--CLR名称空间未定义云云

    分类: WPF

    以下介绍如何创建可用于在多个程序集之间共享的资源字典

    1.新建Wpf自定义控件库,名称为MyContorlLib,项目资源结构图如下:

    2.打开Themes文件夹中的Generic.xaml文件,在根元素中添加如下名称空间:
    xmlns:local="clr-namespace:MyContorlLib"
    因为待会儿要用到该名称空间下的一个类CustomControl1(系统自动生成的,也可以自己创建)
    3.在资源字典中添加资源:

    1. <LinearGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomControl1}, ResourceId=haha}" StartPoint="0,0" EndPoint="0,1">  
    2.         <GradientStop Color="Blue" Offset="0"></GradientStop>  
    3.         <GradientStop Color="White" Offset="0.5"></GradientStop>  
    4.         <GradientStop Color="Red" Offset="0.7"></GradientStop>  
    5.         <GradientStop Color="Firebrick" Offset="1"></GradientStop>  
    6. </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类型的属性:

    1. public static ComponentResourceKey LinearGradientColorBrushKey {  
    2.            get { return new ComponentResourceKey(typeof(CustomControl1),"haha"); }  
    3. }  
     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程序集,重新生成一下试试吧,会成功的。
    至于为何要“重新生成”,我想用不着我多解释把吧,自己想想也就明白了。

  • 相关阅读:
    Reference Counting GC (Part two :Partial Mark & Sweep)
    Reference Counting GC (Part one)
    Union File System
    Linux Cgroups
    Mark Sweep GC
    取模运算
    负数取模怎么算
    牛客【2021寒假集训营第一场】J-一群小青蛙呱蹦呱蹦呱
    记codeforces一个简单模拟题
    筛法求素数
  • 原文地址:https://www.cnblogs.com/changbaishan/p/4045557.html
Copyright © 2011-2022 走看看