WPF使用资源字典组织资源

转载:http://blog.163.com/[email protected]/blog/static/371405262010111413321728/

首先在解决方案资源管理器中添加一个或多个资源词典(资源字典),并向多个资源字典中添加对象资源信息。示例中新建了三个资源字典,并向资源字典中添加了对象资源,代码如下。

第一个资源字典:(第一个资源字典命名为MyDictionary1.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ImageBrush x:Key="MyBrush1"
                TileMode="Tile"
                ViewportUnits="Absolute"
                Viewport="0 0 30 30"
                ImageSource="/image/1.png"
                Opacity="0.9"/>
</ResourceDictionary>

第二个资源字典:(第二个资源字典命名为MyDictionary2.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <LinearGradientBrush x:Key="MyBrush2">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="Red" Offset="0.1"/>
                <GradientStop Color="Green" Offset="0.5"/>
                <GradientStop Color="Blue" Offset="0.9"/>
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
</ResourceDictionary>

第三个资源字典:(第三个资源字典命名为MyDictionary3.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="MyBrush3" Color="Blue"/>
</ResourceDictionary>

在创建了多个资源字典后可以将它们合并到主窗体的资源集合中,示例代码如下。

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyDictionary1.xaml"/>
                <ResourceDictionary Source="MyDictionary2.xaml"/>
                <ResourceDictionary Source="Mydictionary3.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <ImageBrush x:Key="MyBrush"
                    TileMode="Tile"
                    ViewportUnits="Absolute"
                    Viewport="0 0 50 50"
                    ImageSource="/image/1.png"
                    Opacity="0.9"/>
        </ResourceDictionary>
    </Window.Resources>

将多个资源字典和并到窗体资源集合后,就可以像使用窗体中定义的资源一下使用多个资源字典中的资源了,示例代码如下。

<Window x:Class="WPF中的对象集合.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyDictionary1.xaml"/>
                <ResourceDictionary Source="MyDictionary2.xaml"/>
                <ResourceDictionary Source="Mydictionary3.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <ImageBrush x:Key="MyBrush"
                    TileMode="Tile"
                    ViewportUnits="Absolute"
                    Viewport="0 0 50 50"
                    ImageSource="/image/1.png"
                    Opacity="0.9"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button Background="{StaticResource MyBrush1}"></Button>
    </Grid>
</Window>

时间: 2024-08-27 10:57:21

WPF使用资源字典组织资源的相关文章

【WPF学习】第三十五章 资源字典

原文:[WPF学习]第三十五章 资源字典 如果希望在多个项目之间共享资源,可创建资源字典.资源字典只是XAML文档,除了存储希望使用的资源外,不做其他任何事情. 一.创建资源字典 下面是一个资源字典示例,它包含一个资源: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/wi

(WPF)资源字典

资源字典:资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,                   该文档除了存储希望使用的资源之外,不做任何其它的事情. 资源字典的声明: <ResourceDictionary> <LinearGradientBrush x:Key="fadeBrush"> <GradientStop Color="Red" Offset="0"/>

WPF资源字典使用

资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情. 1.  创建资源字典 创建资源字典的过程比较简单,只是将需要使用的资源全都包含在一个xaml文件之中即可.如下面的例子(文件名xxx.xaml,与后面的app.xaml文件中的内容相对应): <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese

WPF合并资源字典

1.合并多个外部资源字典成为本地字典 示例代码 <Page.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="myresourcedictionary1.xaml"/> <ResourceDictionary Source="myresourcedictionary2.xam

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

wpf多程序集之间共享资源字典--CLR名称空间未定义云云 分类: WPF 2012-10-28 10:57 1162人阅读 评论(0) 收藏 举报 以下介绍如何创建可用于在多个程序集之间共享的资源字典 1.新建Wpf自定义控件库,名称为MyContorlLib,项目资源结构图如下: 2.打开Themes文件夹中的Generic.xaml文件,在根元素中添加如下名称空间:xmlns:local="clr-namespace:MyContorlLib"因为待会儿要用到该名称空间下的一个类

WPF 精修篇 管理资源字典

原文:WPF 精修篇 管理资源字典 样式太多  每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="BurlyWood" O

WPF学习笔记-使用自定义资源字典(style)文件

1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTest.resources&

wpf 在引用外部的资源字典

启动的APP.xaml 资源字典: ScrollViewStyle.xaml 引用的界面的xaml <UserControl x:Class="FranceNews.Pages.点位.Views.FranceFilter" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/

资源字典——程序集之间的资源共享

最近在做一个WPF的应用,我想在WPF的应用中使用资源字典应该是很常见的事情,由于是初次设计这种程序,对WPF也不是很熟悉,不免会遇上一些问题,当然很多也是一些基础的低级的,比如本文将要介绍的“资源字典将如何在不同程序集之间进行共享”的问题就是一个很基础的技术点.在我的应用程序的设计中将资源字典作为程序集单独设计为类库还不是很必要,但我想我们常见的系统中这么做应该是很不错的选择吧,所以,我就很想明白此时我应该怎么做,可是在我从网上查找相关资料时发现这方面的介绍不是很多,而且,介绍的也不够清楚,大