资源字典(Pro WPF 学习)

1、创建资源字典

下面是一个资源字典(AppBrushes.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/sadface.jpg" Opacity="0.3">
    </ImageBrush>
</ResourceDictionary>

为应用程序添加资源字典时候,需要确保将Build Action 设置为Page。这样可以保证为了得到最佳性能将资源字典编译为BAML。不过,将资源字典的Build Action 设置为Resource也是非常完美的,这样它会被嵌入到程序集中,但是不会被编译。当然,在运行时解析它的速度要慢一些。

2、使用资源字典

为了使用资源字典,需要将其合并到某些位置的资源集合中。通常合并到应用程序的资源集合中(App.xaml)。

<Application x:Class="Resources.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="AppBrushes.xaml"/>
        <ResourceDictionary Source="WizardBrushes.xaml"/>
      </ResourceDictionary.MergedDictionaries>
此处可以添加自己的资源
<ImageBrush x:Key="GraphicalBrush1" ... ></ImageBrush> </ResourceDictionary> </Application.Resources> </Application>

3、在程序集之间共享资源

  将资源字典编译到一个单独的类库程序集中。资源字典必须放到generic.xaml文件中,且该文件必须在Themes文件夹中。

  在解决方案右键——添加——新建项目——Visual C#——Windows——WPF自定义控件库,可自动生成需要的文件。

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:ResourceLibrary">
 
    <ImageBrush
      x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResources}, ResourceId=SadTileBrush}"
      TileMode="Tile"
      ViewportUnits="Absolute" Viewport="0 0 32 32"
      ImageSource="ResourceLibrary;component/sadface.jpg" Opacity="0.3">      
    </ImageBrush>
</ResourceDictionary>

  ComponentResourceKey 标记扩展
    为从外部程序集加载的资源定义和引用键。 这使得资源查找功能可以在程序集内指定目标类型,而不是在程序集内或类上指定显式的资源字典。

  XAML 特性用法(设置键,精简版)

<object x:Key="{ComponentResourceKey {x:Type targetTypeName}, targetID}" .../>

  XAML 特性用法(设置键,详细版)

<object x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type targetTypeName}, ResourceID=targetID}" .../>

  XAML 特性用法(请求资源,精简版)

<object property="{DynamicResource {ComponentResourceKey {x:Type targetTypeName}, targetID}}" .../>

  XAML 特性用法(请求资源,详细版)

<object property="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type targetTypeName}, ResourceID=targetID}}" .../>

  XAML 值

    targetTypeName 在资源程序集内定义的common language runtime (CLR) 公共类型的名称。
    targetID 资源的键。 在查找资源时,targetID 将与资源的 x:Key 指令类似。

使用资源字典

添加引用

<Window x:Class="Resources.ResourceFromLibrary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:res="clr-namespace:ResourceLibrary;assembly=ResourceLibrary"
    Title="ResourceFromLibrary" Height="300" Width="300"    
    >
  <StackPanel Margin="5">
    <Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=SadTileBrush}}"
            Padding="5" Margin="5"
            FontWeight="Bold" FontSize="14">
      A Resource From ResourceLibrary</Button>
  </StackPanel>
</Window>

  在使用ComponentResourceKey时,必须使用动态资源,不能用静态。

更加容易使用的做法

  可以定义一个静态属性,让他返回需要使用的正确的ComponentResourceKey。通常在组件的类中定义该属性。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace ResourceLibrary
{
    public class CustomResources
    {
        public static ComponentResourceKey SadTileBrush
        {
            get { return new ComponentResourceKey(typeof(CustomResources), "SadTileBrush"); }
        }
    }
}

  现在可以使用 Static 标记扩展访问该属性并应用资源了,而不需要使用很长的ComponentResourceKey

<Button Background="{DynamicResource {x:Static res:CustomResources.SadTileBrush}}"
        Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
      A Resource From ResourceLibrary
</Button>

转载于:https://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Microsoft's Windows Presentation Foundation (WPF) provides you with a development framework for building high-quality user experiences for the Windows operating system. It blends together rich content from a wide range of sources and allows you unparalleled access to the processing power of your Windows computer. Pro WPF 4.5 in C# provides a thorough, authoritative guide to how WPF really works. Packed with no-nonsense examples and practical advice you'll learn everything you need to know in order to use WPF in a professional setting. The book begins by building a firm foundation of elementary concepts, using your existing C# skills as a frame of reference, before moving on to discuss advanced concepts and demonstrate them in a hands-on way that emphasizes the time and effort savings that can be gained. What you’ll learn •Understand the fundamentals of WPF programming from XAML to controls and data flow. •Develop realistic application scenarios to see navigation, localization and deployment in action. •Explore the advanced user interface controls that WPF provides. •Learn to manage documents from within WPF: Text layout, printing, and document packaging are all covered. •Use graphics and multimedia to add punch to your applications Who this book is for This book is designed for developers encountering WPF for the first time in their professional lives. A working knowledge of C# and the basic architecture of .NET is helpful to follow the examples easily, but all concepts will be explained from the ground up. Table of Contents 01.Introducing WPF 02.XAML 03.Layout 04.Dependency Properties 05.Routed Events 06.Controls 07.The Application 08.Element Binding 09.Commands 10.Resources 11.Styles and Behaviors 12.Shapes, Brushes, and Transforms 13.Geometries and Drawings 14.Effects and Visuals 15.Animation Basics 16.Advanced Animation 17.Control Templates 18.Custom Elements 19.Data Binding 20.Formatting 21.Bound Data 22.Data Views 23.Lists, Trees, and Grids 24.Windows Pages and Navigation 25.Menus, Toolbars, and Ribbons 26. Sound and Video 27.3-D Drawing 28.Documents 29. Printing 30.Interacting with Windows Forms 31.Multithreading 32.The Add-in Model 33.ClickOnce Deployment ----------------------------------------------------------- Pro WPF 4th edition,喜欢的朋友请支持正版。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值