• 星空雅梦


    WPF教程(七)其它资源

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

    创建资源字典的过程比较简单,只是将需要使用的资源全都包含在一个xaml文件之中即可。在项目右击选择添加新建项,选中资源词典(WPF)就完成一个.xaml后缀的资源字典文件,我们看下面一个简单的引用例子。

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:String x:Key="string">helloworld</sys:String>
        <sys:Double x:Key="double">3.141592654</sys:Double>
        <ImageBrush x:Key="imagebrush" >Gray</ImageBrush>
    </ResourceDictionary>
    <Window x:Class="OtherResources.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary Source="Dictionary1.xaml"/>
        </Window.Resources>
        <Grid>
            <WrapPanel>        
                <TextBlock Name="textblock" Text="{StaticResource string}" Background="{StaticResource imagebrush}" Margin="10,10,0,0"/>
            </WrapPanel>
        </Grid>
    </Window>

    在Window.Resources下我们可以创建多个资源,包括style样式或者一些属性设置,但不能有多个资源字典,可以合并多个外部资源字典成为本地字典。再前已经创建一个Dictionary1.xaml资源文件,现在我们再创建一个Dictionary2.xaml,如下:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style TargetType="TextBlock" x:Key="textblock">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontFamily" Value="微软雅黑"/>
            <Setter Property="Text" Value="helloworld"/>
        </Style>
    </ResourceDictionary>
    

    如何将两个外部资源词典合为一个本地资源,我们可以利用ResourceDictionary的MergedDictionaries属性。

    <Window x:Class="OtherResources.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionary1.xaml"/>
                    <ResourceDictionary Source="Dictionary2.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources> 
        <Grid>
            <WrapPanel>                  
                <TextBlock Name="textblock" Text="{StaticResource string}" Background="{StaticResource imagebrush}" Margin="10,10,0,0"/>
                <TextBlock Style="{StaticResource textblock}"/>
            </WrapPanel>
        </Grid>
    </Window>

    特别提示:(1)合并字典(MergedDictionaries 集合中的字典)中对同一对象的同一子元素进行定义的时候,会产生覆盖效果:在这种情况下,所返回的资源将来自在 MergedDictionaries 集合中最后一个找到的字典。(2)合并字典(MergedDictionaries 集合中的字典)中对同一对象的不同子元素定义的时候会产生叠加效果。

    总结

    WPF资源暂且先介绍到这里,后续有新的发现会在这章继续更新。

  • 相关阅读:
    LINQ查询表达式(5)
    LINQ查询表达式(4)
    并不对劲的loj2279
    并不对劲的bzoj1563:p1912:[NOI2009]诗人小G
    并不对劲的loj507
    并不对劲的loj2332
    并不对劲的loj6191
    并不对劲的bzoj4552:p2824:[HEOI2016/TJOI2016]排序
    并不对劲的bzoj3924:loj2135:p3345:[ZJOI2015]幻想乡战略游戏
    并不对劲的bzoj3295:[CQOI2011]动态逆序对
  • 原文地址:https://www.cnblogs.com/LiZhongZhongY/p/10870433.html
Copyright © 2020-2023  润新知