zoukankan      html  css  js  c++  java
  • wpf---资源

    1,资源的本质:ResourceDictionary---单元,存储了<字符串,对象>

    2,在XAML中定义资源

     <Window.Resources>
        <ImageBrush x:Key="TileBrush" TileMode="Tile"
                    ViewportUnits="Absolute" Viewport="0 0 32 32"
                    ImageSource="happyface.jpg" Opacity="0.3"></ImageBrush>
      </Window.Resources>


    3,在XAML中使用资源

              静态资源:

     <Button Background="{DynamicResource TileBrush}" Padding="5"
                FontWeight="Bold" FontSize="14" Margin="5"
                  >Uses a Dynamic Resource</Button>
    
        <Button Padding="5" Margin="5" Click="cmdChange_Click"
                FontWeight="Bold" FontSize="14">Change the Brush</Button>
        <Button Background="{StaticResource TileBrush}" Padding="5" Margin="5"
                FontWeight="Bold" FontSize="14"
                  >Uses a Static Resource</Button>


    使用动态资源的时候,会在使用的时候加载,当改变了字典中的对象时,能够立即更新.但是静态的不可以.

    4,资源的层次  从当前元素---父元素---窗口---APP---系统,所以可以在APP定义全局公用的资源.

    5,在代码中查找和更改资源:

    更改资源就是更改字典的值
     this.Resources["TileBrush"] = new SolidColorBrush(Colors.LightBlue);
    
    


    查找资源有两种;一种直接使用上面的. 另外一种可以使用 obj.FindResource


    6,系统资源

          SystemColors

          SystemFonts

         SystemParameters


    获取和使用方法:都有一个key的标志. 这个意思是动态加载 静态类中的这个画刷.

     <Grid>
            <Label Foreground="{DynamicResource {x:Static SystemColors.ActiveCaptionColor}}" Content="Mxb"/>
        </Grid>

    7  程序集共享和资源共享

                 1,创建一个资源字典的XAML文档:生成操作选择Page

    <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>

                 2,使用资源字典,将其合并到资源集合中

    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionary1.xaml"/>
                    <ResourceDictionary Source="Dictionary2.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

    haha

    含义相当于将资源字典的字典和资源本身的字典合并.


                 3,在不同程序集之中共享资源

      首先需要创建一个资源项目:

    一,创建资源程序集.

                     1,创建一个wpf项目,并且将其类型改为类库

                     2,创建文件夹及xaml资源字典,如下所示

                         image

                    3,创建一个备用的类:注意要是Public的类

    namespace ResourceLibrary1//注意命名控件
    {
       public class DicResource
        {
        }
    }

                    4,在资源文件中编辑需要的资源:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:ResourceLibrary1">
        <SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:DicResource},ResourceId=SolidRedBrush}" Color="Red"/>
        <FontFamily x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:DicResource},ResourceId=IconFontFamily}">ResourceLibrary1;component/Fonts/#iconfont</FontFamily>
    </ResourceDictionary>

                      其中 1,要引用当前的程序集的命名空间

                             2,要创建ComponentResourcekey ---以备后用.

    {ComponentResourceKey TypeInTargetAssembly={x:Type local:DicResource},ResourceId=SolidRedBrush}"

                            3,注意引用的URI地址,需要使用的语法格式:

     ResourceLibrary1;component/Fonts/#iconfont

    二, 使用资源程序集

    1,在引用之中引用该程序集

    image

    2,在项目中引用

                     1,在XAML中引用程序集

                  image

                     2,在需要使用资源的地方使用程序集

     <Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource} ,ResourceId=SolidRedBrush}}"
                    FontFamily="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource},ResourceId=IconFontFamily}}"
                    FontSize="100"
    
                    >&#xe697;</Button>

       3,使用Pack引用别的程序集的资源

    <Window x:Class="Resources.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:res="clr-namespace:ResourceLibrary1;assembly=ResourceLibrary1"
            xmlns:res1="clr-namespace:MxbResource;assembly=MxbResource"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
    
        <StackPanel>
            <Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource} ,ResourceId=SolidRedBrush}}"
                    FontFamily="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource},ResourceId=IconFontFamily}}"
                    FontSize="100"
    
                    >&#xe697;</Button>
            <Image Source="pack://application:,,,/MxbResource;component/sadface.jpg" Width="100" Height="100"/>
            <Button Content="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res1:MxbRes},ResourceId=MxbImage1}}" Width="100"></Button>
    
        </StackPanel>
    </Window>

    4,使用c#后台代码引用资源

    <StackPanel>
            <Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource} ,ResourceId=SolidRedBrush}}"
                    FontFamily="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DicResource},ResourceId=IconFontFamily}}"
                    FontSize="100"
    
                    >&#xe697;</Button>
            <Image Source="pack://application:,,,/MxbResource;component/sadface.jpg" Width="100" Height="100"/>
            <Button Content="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res1:MxbRes},ResourceId=MxbImage1}}" Width="100"></Button>
            <Button Content="{DynamicResource {x:Static local:Window1.ComponentImageKey}}">
    
            </Button>
        </StackPanel>
      public static ComponentResourceKey ComponentImageKey
            {
                get
                {
                    return new ComponentResourceKey(typeof(MxbRes), "MxbImage1");
                }
            }
    /

    //添加一个静态对象用于使用.注意,Resource这个解析将会解析成实际对象

     public Window1()
            {
                InitializeComponent();
    
               object obj= this.FindResource(ComponentImageKey);
            }

    也就是说资源有两种形式的Key 一种是普通的,一种是ComponentResourceKey,用于跨界读取.                

    另外,必须创建文件夹和xaml文件名为Themes 和 generic.xaml的资源字典文件.

    //测试项目在编程宝典源码第10章window1和自己建的资源类库MxbLibrary.       

  • 相关阅读:
    Angular语法(三)——数据绑定
    Angular语法(二)——模板语法
    Angular语法(一)——展示数据
    Angular常用指令
    windows下启动redis
    WPF实现弹幕
    微信获得用户信息
    拉普拉斯变换
    Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering
    Python之并行
  • 原文地址:https://www.cnblogs.com/frogkiller/p/12930012.html
Copyright © 2011-2022 走看看