第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

<ResourceDictionary
    xmlns="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
<ResourceDictionary
    xmlns="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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值