WPF資源維持方法介紹
通過對WPF的應(yīng)用,開發(fā)人員會發(fā)現(xiàn),在一個程序中WPF資源的存在數(shù)量是非常驚人的。那么,如何組織我們的WPF資源。在WPF中可以有很多種組織資源的方式。#t#
每個元素維持自己的WPF資源。擁有Resources屬性的對象有很多種類型,比如Application、Window、Page、Style、FrameworkTemplate、FrameworkElement、FrameworkContentElement等。在這種模式種每種元素維持自己所需要的資源,而不顧忌資源是否可以共享,比如兩個Button:
- < Button x:Name="btn1"
- Content="button 1"
- Foreground="{Dynamic
Resource redBrush}"> - < Button.Resources>
- < SolidColorBrush
x:Key="redBrush" Color="Red"/> - < /Button.Resources>
- < /Button>
- < Button x:Name="btn2"
- Content="button 2"
- Foreground="{Dynamic
Resource redBrush}"> - < Button.Resources>
- < SolidColorBrush
x:Key="redBrush" Color="Red"/> - < /Button.Resources>
- < /Button>
這造成的問題是,WPF資源不能共享而重復(fù)創(chuàng)建一些相同的資源影響效率。