WPF and Silverlight 学习笔记(十六):WPF资源(Resource)(1)

news/2024/7/7 19:02:36

一、什么是资源

通常使用 WPF 资源作为重用通常定义的对象和值的简单方法。例如定义一种可以复用的单色的Brush对象,按钮的背景及矩形的填充颜色均使用此Brush:

   1: <Window x:Class="WPFResource.WinBasicResource"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="Basic Resource" Height="200" Width="300">
   5:     <Window.Resources>
   6:         <SolidColorBrush x:Key="myBrush" Color="Gold" />
   7:     </Window.Resources>
   8:     <StackPanel>
   9:         <Button Margin="5" Content="Sample Button" Background="{StaticResource myBrush}" />
  10:         <Rectangle Margin="5" Width="100" Height="100" Fill="{StaticResource myBrush}" />
  11:     </StackPanel>
  12: </Window>

在WPF中资源通常用作“样式”(Style)、样式模板、数据模板等。

二、资源的定义及XAML中引用

资源可以定义在以下几个位置:

  • 应用程序级资源:定义在App.xaml文件中,作为整个应用程序共享的资源存在

在App.xaml文件中定义:

   1: <Application x:Class="WPFResource.App"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     StartupUri="Window1.xaml">
   5:     <Application.Resources>
   6:         <SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
   7:     </Application.Resources>
   8: </Application>

在ApplicationResourceDemo.xaml文件(窗体)中使用App.xaml中定义的Resource

   1: <Window x:Class="WPFResource.ApplicationResourceDemo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="Application Resource Demo" Height="300" Width="300">
   5:     <StackPanel>
   6:         <Button Margin="5" Background="{StaticResource myGoldBrush}">Sample Button</Button>
   7:     </StackPanel>
   8: </Window>
  • 窗体级资源:定义在Window或Page中,作为一个窗体或页面共享的资源存在
   1: <Window x:Class="WPFResource.WindowResourceDemo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="WindowResourceDemo" Height="300" Width="300">
   5:     <Window.Resources>
   6:         <SolidColorBrush x:Key="myRedBrush" Color="Red" />
   7:     </Window.Resources>
   8:     <StackPanel>
   9:         <Button Margin="5" Background="{StaticResource myRedBrush}">Sample Button</Button>
  10:     </StackPanel>
  11: </Window>
  • 文件级资源:定义在资源字典的XAML文件中,再引用

在Visual Studio的WPF应用程序项目中,添加“资源字典(Resource Dictionary)”类型的项

AddResourceDictionary

在其XAML文件中定义:

   1: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   2:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   3:     <SolidColorBrush x:Key="myWhiteBrush" Color="White" />
   4: </ResourceDictionary>

在FileResourceDemo.xaml文件(窗体)中,将其注册为窗体级的资源,并引用

   1: <Window x:Class="WPFResource.FileResourceDemo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="FileResourceDemo" Height="300" Width="300">
   5:     <Window.Resources>
   6:         <ResourceDictionary Source="MyResourceDictionary.xaml" />
   7:     </Window.Resources>
   8:     <StackPanel>
   9:         <Button Margin="5" Background="{StaticResource myWhiteBrush}">Sample Button</Button>
  10:     </StackPanel>
  11: </Window>
  • 对象(控件)级资源:定义在某个ContentControl中,作为其子容器、子控件共享的资源

在Button中定义一个资源,供Button内的Content控件使用

   1: <Window x:Class="WPFResource.ControlResourceDemo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="ControlResourceDemo" Height="300" Width="300">
   5:     <StackPanel>
   6:         <Button Margin="5">
   7:             <Button.Resources>
   8:                 <SolidColorBrush x:Key="myGreenBrush" Color="Green" />
   9:             </Button.Resources>
  10:             <Button.Content>
  11:                 <TextBlock Text="Sample Text" Background="{StaticResource myGreenBrush}" />
  12:             </Button.Content>
  13:         </Button> 
  14:     </StackPanel>
  15: </Window>

三、XAML解析资源的顺序

在XAML中解析资源按照由引用资源的控件向外层容器依次调用资源。例如在在应用程序级别、窗体级别及对象级别分为定义x:Key相的同资源:

在App.xaml文件中:

   1: <Application x:Class="WPFResource.App"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     StartupUri="MultiResourceReference.xaml">
   5:     <Application.Resources>
   6:         <!-- 应用程序级资源 -->
   7:         <SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
   8:         <SolidColorBrush Color="Blue" x:Key="myBrush" />
   9:     </Application.Resources>
  10: </Application>

在窗体的XAML文件中:

   1: <Window x:Class="WPFResource.MultiResourceReference"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="MultiResourceReference" Height="265" Width="300">
   5:     <Window.Resources>
   6:         <!-- 窗体级资源 -->
   7:         <SolidColorBrush Color="White" x:Key="myWhiteBrush" />
   8:         <SolidColorBrush Color="Green" x:Key="myBrush" />
   9:     </Window.Resources>
  10:     <StackPanel>
  11:         <!-- 使用应用程序级定义的资源 -->
  12:         <Button Margin="5" Content="Sample Button" Background="{StaticResource myGoldBrush}" />
  13:         
  14:         <!-- 使用窗体级定义的资源 -->
  15:         <Button Margin="5" Content="Sample Button" Background="{StaticResource myWhiteBrush}" />
  16:         
  17:         <!-- 窗体级资源的值覆盖应用程序级资源的值 -->
  18:         <Button Margin="5" Content="Sample Button" Background="{StaticResource myBrush}" />
  19:         
  20:         <StackPanel Background="#FF999999">
  21:             <StackPanel.Resources>
  22:                 <!-- 对象级资源 -->
  23:                 <SolidColorBrush Color="Yellow" x:Key="myYellowBrush" />
  24:                 <SolidColorBrush Color="Red" x:Key="myBrush" />
  25:             </StackPanel.Resources>
  26:  
  27:             <!-- 使用应用程序级定义的资源 -->
  28:             <Button Margin="5" Content="Sample Button" Background="{StaticResource myGoldBrush}" />
  29:  
  30:             <!-- 使用窗体级定义的资源 -->
  31:             <Button Margin="5" Content="Sample Button" Background="{StaticResource myWhiteBrush}" />
  32:  
  33:             <!-- 使用对象级定义的资源 -->
  34:             <Button Margin="5" Content="Sample Button" Background="{StaticResource myYellowBrush}" />
  35:  
  36:             <!-- 使用对象级定义的资源覆盖窗体级、应用程序级定义的资源 -->
  37:             <Button Margin="5" Content="Sample Button" Background="{StaticResource myBrush}" />
  38:         </StackPanel>
  39:     </StackPanel>
  40: </Window>

MultiResourceReference

未完待续…

本系列博客索引页

转载于:https://www.cnblogs.com/DragonInSea/archive/2009/04/21/1440360.html


http://www.niftyadmin.cn/n/547265.html

相关文章

EasyTools的IframeAdapt和SubmitPanel详解

今天跟大家分享下EasyTools的IframeAdapt控件和SubmitPanel控件,由于这两个控件比较简单&#xff0c;所以就一次性都写出来了&#xff0c;自此&#xff0c;EasyTools的控件都讲解完毕了&#xff0c;除了那个MyTextBox&#xff0c;MyTextBox的使用方法跟AlertTextBox的使用方法一…

html给button加边框,iOS 如何给UIButton设置边框【原创】

在xcode4之前的UIButton都自带边框的&#xff0c;后来我直接升级到6之后发现UIButton的边框居然不见了&#xff0c;只有手动设置了。下面直接用代码来说明如何设置边框UIButton *dynamicCode_btn;[_dynamicCode_btn.layer setMasksToBounds:YES];[_dynamicCode_btn.layer setCo…

python(22)总结下最近遇到的编码问题

最近爬取&#xff0c;或者解析网页是总是遇到编码问题&#xff08;我的版本&#xff1a;python2.7&#xff09; 一、常见异常&#xff1a;UnicodeEncodeError: ascii codec cant encode character u\xb4 in position 0: ordinal not in range(128) 常见解决方案&#xff1a;在代…

计算机病毒 评课,关于计算机教学计划4篇

关于计算机教学计划4篇时间过得真快&#xff0c;总在不经意间流逝&#xff0c;又将迎来新的工作&#xff0c;新的挑战&#xff0c;写一份计划&#xff0c;为接下来的工作做准备吧&#xff01;可是到底什么样的计划才是适合自己的呢&#xff1f;下面是小编整理的计算机教学计划4…

使用Apache Mesos打造分布式资源调度系统

Netflix使用Apache Mesos运行了一系列批处理、流式处理&#xff0c;以及服务类型的工作负载。两年多来&#xff0c;我们创建了层出不穷的用例&#xff0c;例如实时异常检测、批处理作业的训练和模型构建、机器学习编排&#xff0c;以及基于Node.js的微服务。最近发布的Apache M…

进行数据库设计时不妨请参考如下几个技巧

一&#xff1a;原始单据与实体之间的关系 可以是一对一、一对多、多对多的关系。在一般情况下&#xff0c;它们是一对一的关系&#xff1a;即一张原始单据对应且只对应一个实体。在特殊情况下&#xff0c;它们可能是一对多或多对一的关系&#xff0c;即一张原始单证对应多个实体…

艺考可以报考清华的计算机系吗,孟令昊:成为山东高考状元的艺术生,最终被清华大学计算机系录取...

前一段时间&#xff0c;关于山东省高考状元孟令昊的报道不断&#xff0c;关注热度十分高。今年是山东“新高考”第一年&#xff0c;除了语文、数学、外语3门必考科目外&#xff0c;考生还要在其他6门课中选择3科&#xff0c;总分仍然是750分&#xff0c;但不再有文理分科。一、…

js键盘事件全面控制

主要分四个部分第一部分&#xff1a;浏览器的按键事件第二部分&#xff1a;兼容浏览器第三部分&#xff1a;代码实现和优化第四部分&#xff1a;总结 第一部分&#xff1a;浏览器的按键事件 用 js实现键盘记录&#xff0c;要关注浏览器的三种按键事件类型&#xff0c;即keydown…