阅读 100

WPF教程九:理解WPF中的对象资源

  在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源。

  这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集中),这个是资源是我们想在公共的地方写一个对象让其他元素重复使用。

  先贴个例子:

"NETResource.MainWindow"
        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:local="clr-namespace:NETResource"0
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
        "RedBrushButtonBackground" Color="Red" />
    
    
        
            "BlueBrushButtonBackground" Color="Blue"/>
        
        
            

显示效果:  

从例子中我们看几个资源的关键点,我们再Window元素和Grid元素下添加两个颜色画刷资源,使用x:key标识。上面2个Button都使用了2个不同层级的父元素资源。WPF中有个设计比较好的地方就是元素可以使用父元素的资源。这样我们都把资源放在Window节点下,公用这些资源。

资源定义好之后,再使用时,可以指定以静态的方式使用资源,还是以动态的方式使用资源。

差别就是静态资源只从资源集合获取对象一次,对象的任何变化都会得到消息,而动态资源每次需要用到对象时都会重新从资源集合中查找对象。注意动态资源是每次需要用到对象时才会去资源集合查找,所以在资源非常大,且非常复杂的时候,可以用动态资源的方式可以提高第一次加载窗口的速度(因为没有解析资源标记的过程),其他任何使用都不建议使用动态资源。使用数据绑定去实现。

"NETResource.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:local="clr-namespace:NETResource"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    
        "ButtonBrushBackground" Color="DarkBlue"/>
    
     
        "120">
            
using System.Windows;
using System.Windows.Media;

namespace NETResource
{
    /// 
    /// Window1.xaml 的交互逻辑
    /// 
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void SetButtonBackgroudButton(object sender, RoutedEventArgs e)
        {
            //修改资源指向的对象。只有动态资源会受到影响,因为动态资源每次使用值的时候,都会重新读取。静态资源不会,所以静态资源不受影响。
            //修改样式1
            SolidColorBrush solid = new SolidColorBrush(Colors.Red);
            solid.Opacity = 0.3;
            this.Resources["ButtonBrushBackground"] = solid;
            //修改样式2
            // LinearGradientBrush linear = new LinearGradientBrush(Colors.Red, Colors.Blue, 90.0);
            // this.Resources["ButtonBrushBackground"] = linear;
        }

        private void UpdataButtonBackgroundButton(object sender, RoutedEventArgs e)
        {
            //修改资源对象的值,资源没有改变,只是改变了资源的值,所以2个按钮都受到影响。
            var brush = this.Resources["ButtonBrushBackground"];
            if (brush is SolidColorBrush)
            {
                ((SolidColorBrush)brush).Color = Colors.LightBlue;
            }

        }
    }
}

如果有些资源是所有窗体都共享的,建议写在Application的.Resources下。

"NETResource.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NETResource"
             StartupUri="Window1.xaml">
    
        "TitleBrush" Color="Red"/>
    
"NETResource.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:local="clr-namespace:NETResource"
        mc:Ignorable="d" 
        Title="WPF教程九:理解WPF中的资源"  
        Height="450" Width="800">
    
    
        "ButtonBrushBackground" Color="DarkBlue"/>
    
    
        "WPF教程九:理解WPF中的资源"  Foreground="{StaticResource TitleBrush}"/>    
    

资源我们都会使用了,接下来需要归类整理我们的资源,使用资源字典:

我们在工程上右键=》添加=》资源字典=》设置名字为AppBrushes.xaml=》保存

添加代码如下:

"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:NETResource">
    "DictionaryTitleBrush" Color="Beige"/>

在App.xaml中添加对资源字典的使用:

"NETResource.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NETResource"
             StartupUri="Window1.xaml">
     
        
            
                "AppBrushes.xaml"/>
            
            "TitleBrush" Color="Red"/>
          
    

这样资源字典就可以被访问了。

我们创建一个button按钮使用资源字典内的样式

  

这里有一个小技巧:我们还记得上一章讲的程序集资源,我们作为二进制存放我们需要保存的文件。那么我们创建的这个资源字典也属于文件,也可以被编译在程序集内,程序集资源又有BAML和Resources资源。BAML是编译后的,Resources是保持当前状态被嵌入到程序里面的。所以我们把资源字典的属性设置为page,会减少一个运行时解析过程。

跨程序集使用资源:这个可程序集资源的跨程序集使用。

一定要分清楚,什么是二进制资源(程序集资源持久化),什么是对象资源(公共部分重复使用的对象)。我们手动创建引用其他库的资源字典。

 在新建资源DLL的时候,我没有找到直接新建添加引用之后的类库,所以我用以下2种方法种的一种来创建程序集,我使用的是第一种:

1)创建WPF程序,然后删除他下面的App.config、App.xaml、MainWindow.xaml 和Properties下的Rsources.resx、Settings.settings,工程右键=》属性=》应用程序=》输出类型=》类库。用于保留自动对PresentationCore、PresentationFramlework、WindowsBase的引用。

2)添加类库程序,然后添加=》引用=》PresentationCore、PresentationFramlework、WindowsBase。这三个的引用。

添加DLL完毕后,在窗体程序中添加对DLL库的引用。整个目录引用关系结构如下: 

 

 

 现在开始写代码,

首先是ResourceLibrary库。这个是我们的资源库,里面存放我们的资源文件。目前是一个xaml的资源字典。需要我们新建出来。

代码如下:

"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ResourceLibrary">
    "ReusableTitle" Color="Yellow"/>

 

而后是引用的App,在App.xaml下添加跨程序集的资源字典(使用pack: URI): 

"WPFUsingResourceLib.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFUsingResourceLib"
             StartupUri="MainWindow.xaml">
    
        
            
                "pack://application:,,,/ResourceLibrary;component/ReusableDictionary.xaml"/>
            
        
    

 

 在Window窗体中使用以添加引用的资源:

"WPFUsingResourceLib.MainWindow"
        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:local="clr-namespace:WPFUsingResourceLib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
     
        

 

效果图如下:

好啦,这一篇就写这么多把。主要是就是对象资源的使用,静态和动态资源的差别,跨程序集资源,元素可以使用父类资源。这篇主要是理解就行,后面会写软件,用于演示如何更好的使用这些内容。

 

原文:https://www.cnblogs.com/duwenlong/p/14612070.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐