(轉載)資源字典(Pro WPF 學習)

原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.htmlhtml

一、建立資源字典post

下面是一個資源字典(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也是很是完美的,這樣它會被嵌入到程序集中,可是不會被編譯。固然,在運行時解析它的速度要慢一些。ui

二、使用資源字典spa

爲了使用資源字典,須要將其合併到某些位置的資源集合中。一般合併到應用程序的資源集合中(App.xaml)。code

複製代碼
<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>
複製代碼

三、在程序集之間共享資源component

  將資源字典編譯到一個單獨的類庫程序集中。資源字典必須放到generic.xaml文件中,且該文件必須在Themes文件夾中。xml

  在解決方案右鍵——添加——新建項目——Visual C#——Windows——WPF自定義控件庫,可自動生成須要的文件。htm

generic.xaml文件blog

複製代碼
<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>
相關文章
相關標籤/搜索