zoukankan      html  css  js  c++  java
  • WPF ResourceDictionary的使用

    作用:一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便。最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样。

    初衷:就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情。

    1.  创建资源字典

          创建资源字典的过程比较简单,只是将需要使用的资源全都包含在一个xaml文件之中即可。如下面的例子(文件名test.xaml,与后面的app.xaml文件中的内容相对应):

    <?xml version="1.0" encoding="utf-8"?>

    <!--This file is auto generated by XDraw.-->

    <!--Do not modify this file directly, or your changes will be overwritten.-->

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <LinearGradientBrush x:Key="FadeBrush">

           <GradientStop Color="Red" Offset="0"/>

           <GradientStop Color="Gray" Offset="1"/>

        </LinearGradientBrush>

    </ResourceDictionary>

    说明:在创建资源的时候要确保资源文件的编译选项为page,这样就能够保证XAML资源文件最终能够编译为baml文件。但是如果设置为Resource也是一个不错的选择,这样它能够嵌入到程序集中,但是不被编译,当然其解析的速度回稍微慢一点。

    2.  使用资源字典

    2.1  集成资源

         要是用资源字典,首先要将资源字典集成到应用程序的某些资源集合中。一般的做法都是在app.xaml文件中进行集成。代码如下:

        <ResourceDictionary>

                <ResourceDictionary.MergedDictionaries>

                    <ResourceDictionary Source="Test.xaml"/>

                </ResourceDictionary.MergedDictionaries>

            </ResourceDictionary>

    2.2  使用资源

         集成之后就可以在当前的工程中使用这些资源了。使用方法如下:

    <Window x:Class="HelloWpf.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Title="MainWindow" Height="340" Width="406" WindowStartupLocation="CenterScreen"

            Icon="SC.ico" >

       

        <Grid Height="304" Width="374">

            <Grid.RowDefinitions>

                <RowDefinition Height="Auto"/>

                <RowDefinition Height="Auto"/>

                <RowDefinition Height="*"/>

            </Grid.RowDefinitions>

            <Button Margin="121,30,107,230" Grid.Row="2" Click="Button_Click"  Background="{StaticResource FadeBrush}">

            </Button>

        </Grid>

    </Window>

    使用资源的方法比较简单只需要使用StaticResource 关键字去添加即可。

    2.1内部集成

    或者内部集成在窗口中引用外部资源,注意:在Window中添加外部样式需要指定key,而Application中则不需要,所以这里FadeBrush就是引用外部样式test.xaml的key

    <Window x:Class="MSSQLDocCreator.MainWindow"  

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  

           xmlns:local="clr-namespace:MSSQLDocCreator"  

           Title="MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">  

           <Window.Resources>  

           <ResourceDictionary x:Key="FadeBrush">  

           <ResourceDictionary.MergedDictionaries>  

            <ResourceDictionary Source="test.xaml" />  

            </ResourceDictionary.MergedDictionaries>  

           </ResourceDictionary>  

           </Window.Resources>  

            ....   

          </Window>  

        将样式应用到窗口的布局上

        <Grid Resources="{StaticResource rdStyle}">   

        </Grid>   

    3.  总结:

         使用资源字典的主要原因有两个:

        a. 提供皮肤功能。

        b. 存储需要被本地话的内容(错误消息字符串等,实现软编码)

         使用过程也比较简单,归纳起来主要有下面几个步骤:

        1. 创建资源字典文件

        2. 资源字典集成

        3. 使用字典中的资源

  • 相关阅读:
    tomcat 启动 报错Neither the JAVA_HOME nor the JRE_HOME environment variable is definedtemp
    tomcat linux 加入服务自动启动
    registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    tomcat 8 加 struts2的 java.lang.NoSuchFieldException: resourceEntries
    tomcat Can't create cache file!
    tomcat 部署时修改服务器时间
    tomcat java变量环境设置
    scrapy 动态IP、随机UA、验证码
    scrapy xpath、正则表达式、css选择器
    Saltstack windows可视化操作(十四)
  • 原文地址:https://www.cnblogs.com/wlming/p/4560448.html
Copyright © 2011-2022 走看看