资源

WPF快速指导1:资源
    本文摘要:
    1:资源应用场景;
    2:静态资源和动态资源;
    3:Application.Current.Resources和Application.Current.Resources.MergedDictionaries
    4:路径


一:资源的应用场景


场景1:格式化界面显示元素
    所谓格式化界面显示元素,就是使用统一的风格来定义软件的每个界面。
    要满足本需求,只需要在App.xaml中如下定义资源

<Application.Resources>
        <Style TargetType="TextBlock" x:Key="TitleText">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
        <Style TargetType="TextBlock" x:Key="Label">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
</Application.Resources>

同时,在每个页面如下引用资源即可:

    <StackPanel>
        <TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
        <TextBlock Style="{StaticResource Label}">Label</TextBlock>
    </StackPanel>

场景2:动态更新界面风格
    要动态更新界面风格,首先需要定义多种界面风格。假设有Sytle1和Style2两种风格,其中Style1在Style1.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="TitleText">
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
    <Style TargetType="TextBlock" x:Key="Label">
        <Setter Property="Background" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
</ResourceDictionary>

Style2在Style2.xaml中定义(假设将Style1中的Blue改为Yellow,12改为24,不再列出)。那么,在页面中,我们如下引用:

    <StackPanel>
        <TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
        <TextBlock Style="{DynamicResource Label}">Label</TextBlock>
        <Button Click="Button1_Click">Style1</Button>
        <Button Click="Button2_Click">Style2</Button>
    </StackPanel>

同时,后台代码为:

        ResourceDictionary style1;
        ResourceDictionary style2;
        
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            style1 = new ResourceDictionary();
            style1.Source = new Uri(@"Resouce/Style1.xaml", UriKind.Relative);
            Application.Current.Resources = style1;
        }
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            style2 = new ResourceDictionary();
            style2.Source = new Uri(@"Resouce/Style2.xaml", UriKind.Relative);
            Application.Current.Resources = style2;
        }

    如此一来,我们便完成动态的界面风格变化。


二:静态资源和动态资源

    在上文的场景二示例中,如果将XAML中的

        <TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
        <TextBlock Style="{DynamicResource Label}">Label</TextBlock>

    换成

        <TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
        <TextBlock Style="{StaticResource Label}">Label</TextBlock>

    我们会发现界面的风格根本没有得到改变。这里我们引出静态资源和动态资源最重要的一个区别:静态资源不基于运行时行为进行重新求值,而动态资源在运行时加载。
    关于静态资源和动态资源其它区别请查看MSDN。


三:Application.Current.Resources和Application.Current.Resources.MergedDictionaries

    先来看这两个变量的原型:
    Application.Current.Resources的原型是一个ResourceDictionary。
    Application.Current.Resources.MergedDictionaries是一个Collection<ResourceDictionary> 。
    从本质上来讲,这两个变量没有区别,MergedDictionaries是在表现形式上,在运行时扩展系统的资源。
    我们再来看上文中运行时动态改变界面的示例,我们通过动态给Application.Current.Resources赋值,来改变界面风格。
    在实际使用中,必不要这么做。因为不管你是否需要在运行时动态更新部分界面风格,有些资源是肯定不变的。也就是说,一个系统,必定已经存在一个资源文件,即,最好不要在运行时改变Application.Current.Resources。那么,实际要做的,就是动态的增加或者删除Application.Current.Resources.MergedDictionaries就可以了。


四:路径

  第一种:

            imgContent.Source = new BitmapImage(new Uri("Content.jpg", UriKind.Relative)); 
            imgResource.Source = new BitmapImage(new Uri("Resource.jpg", UriKind.Relative));

  第二种:

            imgContent.Source = new BitmapImage(new Uri("pack://application:,,,/Content.jpg")); 
            imgResource.Source = new BitmapImage(new Uri("pack://application:,,,/Resource.jpg"));

  第三种:

            imgContent.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/Content.jpg")); 

  最后一点需要说说的是路径的问题,关于路径,在WPF中有几种表示方法:

  第一种和第二种都可以访问相对WPF资源路径的Resource和Content资源。第三种方式可以访问运行目录下的Content资源文件以及完全松散的文件。完全松散的文件指那些没有添加到项目中,只是拷贝在程序目录中的文件。

  应用程序根本不知道它的存在。pack://application:,,,/Content.jpg表示当前项目的资源。它是pack://application:,,,/DllName;Component/Content.jpg的简写。将DllName替换成其他程序集,就可以访问其他程序集的资源。

  pack://SiteOfOrigin:,,,/Content.jpg表示从部署位置访问文件。

  pack URI格式是XML文件规范的一部分,具体格式如下 pack://packageURI/partPath。PackageURI实际上是在URI中放一个URI,它是把反斜杠都变成了逗号。packageURI的WPF资源路径可以志向一个XPS文档,例如file : /// c: /Document . xps会被编码为file:...c:,Document.xps。在WPF程序中有两种URI系统是特别处理的:

  siteOfOrigin:/// 编码后siteOfOrigin:,,,

  application:/// 编码后application:,,,

  3个逗号其实是反斜杠编码过来的。

原文地址:https://www.cnblogs.com/ysharp/p/2731677.html