zoukankan      html  css  js  c++  java
  • WPF资源

     

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

    一、资源的定义及使用

    1. 应用程序级资源:

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

    1 <Application x:Class="DeepXAML.App"
    3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4              StartupUri="MainWindow.xaml">
    5     <Application.Resources>
    6         <SolidColorBrush Color="Red" x:Key="redBrush"></SolidColorBrush>
    7     </Application.Resources>
    8 </Application>

    使用应用程序集资源

    01 <Window x:Class="DeepXAML.MainWindow"
    03         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    04         xmlns:local="clr-namespace:DeepXAML"       
    05         xmlns:sys="clr-namespace:System;assembly=mscorlib"
    06         Title="MainWindow" Height="250" Width="450">   
    07     <StackPanel x:Name="stackPanel">
    08         <Button Background="{StaticResource ResourceKey=redBrush}">test app resource</Button>
    09     </StackPanel>
    10 </Window>

    2. 窗体级资源:定义在Window或Page中,作为一个窗体或页面共享的资源存在

    01 <Window x:Class="DeepXAML.MainWindow"
    03         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    04         xmlns:local="clr-namespace:DeepXAML"       
    05         xmlns:sys="clr-namespace:System;assembly=mscorlib"
    06         Title="MainWindow" Height="250" Width="450">  
    07     <Window.Resources>
    08         <SolidColorBrush Color="Red" x:Key="redBrush"></SolidColorBrush>
    09     </Window.Resources>
    10     <StackPanel x:Name="stackPanel">
    11         <Button Background="{StaticResource ResourceKey=redBrush}">test app resource</Button>
    12     </StackPanel>
    13 </Window>

    3. 文件级资源:定义在资源字典的XAML文件中,再引用

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

    2                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    3     <SolidColorBrush Color="Red" x:Key="redBrush"></SolidColorBrush>
    4 </ResourceDictionary>
    01 <Window x:Class="DeepXAML.MainWindow"
    03         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    04         xmlns:local="clr-namespace:DeepXAML"       
    05         xmlns:sys="clr-namespace:System;assembly=mscorlib"
    06         Title="MainWindow" Height="250" Width="450">  
    07     <Window.Resources>
    08         <ResourceDictionary Source="Skin1.xaml"></ResourceDictionary>
    09     </Window.Resources>
    10     <StackPanel x:Name="stackPanel">
    11         <Button Background="{StaticResource ResourceKey=redBrush}">test app resource</Button>
    12     </StackPanel>
    13 </Window>

    4.对象(控件)级资源:定义在某个ContentControl中,作为其子容器、子控件共享的资源

    01 <Window x:Class="DeepXAML.MainWindow"
    03         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    04         xmlns:local="clr-namespace:DeepXAML"       
    05         xmlns:sys="clr-namespace:System;assembly=mscorlib"
    06         Title="MainWindow" Height="250" Width="450">  
    07      <StackPanel x:Name="stackPanel">
    08         <StackPanel.Resources>
    09             <ResourceDictionary Source="Skin1.xaml"></ResourceDictionary>
    10         </StackPanel.Resources>
    11         <Button Background="{StaticResource ResourceKey=redBrush}">test app resource</Button>
    12     </StackPanel>
    13 </Window>
    14   
    15 二、资源文件解析的顺序
    1 这个顺序和层叠样式表类似,优先级从高到底为:对象级,窗体级,应用程序集。静态资源(StaticResource)和动态资源(DynamicResource)

    资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。

    通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。

     

    三、静态资源(StaticResource)和动态资源(DynamicResource)

    资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。

    通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。

    Background="{DynamicResource redBrush}"
     
    private void Button_Click(object sender, RoutedEventArgs e)
    {
       SolidColorBrush brush = new SolidColorBrush(Colors.Green);
       this.Resources["redBrush"] = brush;
    }

     

  • 相关阅读:
    几个简单递归js 实现
    js中阻止事件冒泡
    判断是否IE 最酷的代码
    C# Hashtable 读取 key 和 value
    ubuntu搭建jdk+jenkins
    Shell逻辑运算符及表达式
    Linux命令的返回值
    python调用c/c++时传递结构体参数
    Git错误总结
    Bash Shell中命令行选项/参数处理
  • 原文地址:https://www.cnblogs.com/luluping/p/2039501.html
Copyright © 2011-2022 走看看