zoukankan      html  css  js  c++  java
  • Freeable resource的特点

    继承自Freeable的类(如Brush)拥有基本的改变跟踪机制,实质上就是会加一个change even handler给拥有它的目标类,它自身来抛出时间通知。

    这样就是让即使 Brush作为StaticResource,其自身属性改变时,也能在运行时更新; 在确定我们的系统中brush 不会变化时,应该把brush freeze掉,已减少不必要得负担,提高性能。对于DynamicResource,不能用frozen 的资源。

    例子:

    <Window x:Class="FrozenPerf.Window1"

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

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

     xmlns:local="clr-namespace:FrozenPerf"

     Title="Window1">

     <Window.Resources>

     <SolidColorBrush x:Key="brush" Color="AliceBlue"/>

     <LinearGradientBrush x:Key="linearGradientBrush" StartPoint="0,0" EndPoint="0,1">

     <GradientStop Color="#FF6593CF" Offset="0" />

     <GradientStop Color="#FF6593CF" Offset="0.2" />

     <GradientStop Color="#FFADD1FF" Offset="1" />

     </LinearGradientBrush>

     <LinearGradientBrush x:Key="CommandButtonInnerPressedBrush" StartPoint="0,1" EndPoint="0,0">

     <GradientStop Color="#FFFFFDF5" Offset="0" />

     <GradientStop Color="#FFE9E6DE" Offset=".49" />

     <GradientStop Color="#FFD7D4CC" Offset=".5" />

     <GradientStop Color="#FFE1DED6" Offset="1" />

     </LinearGradientBrush>

     </Window.Resources>

     <Grid>

     <StackPanel>

     <Button Click="Button_Click">Change</Button>

    <TextBox Width="100" Height="100" Text="{Binding Text}" Background="{StaticResource linearGradientBrush}"/>

     </StackPanel>

    </Grid>

    </Window>

     在button click的时候,不换对象,而只是更改对象的属性;这是颜色会跟着变化(当frozen掉brush时,不能在运行时改变其属性);

    而如果更换对象,则不会起作用,只有在dynamicResource后才能起作用。

     privatevoid Button_Click(object sender, RoutedEventArgs e)

    {

    LinearGradientBrush linearBrush = (LinearGradientBrush)this.Resources["linearGradientBrush"];

    LinearGradientBrush secondlinearBrush = (LinearGradientBrush)this.Resources["CommandButtonInnerPressedBrush"];

    linearBrush.GradientStops = secondlinearBrush.GradientStops;

    linearBrush.StartPoint = secondlinearBrush.StartPoint;

    linearBrush.EndPoint = secondlinearBrush.EndPoint;

    //no effect except dynamicResource

    //this.Resources["linearGradientBrush"]=secondlinearBrush

    }

    我们知道dynamicResource 很耗性能:

    A static resource grabs the object from the resources
    collection once. Depending on the type of object (and the way it’s used), any changes you
    make to that object may be noticed right away. However, the dynamic resource looks the
    object up in the resources collection every time it’s needed. That means you could place an
    entirely new object under the same key and the dynamic resource would pick up your change.

    是否可以对于freezable的资源,用StaticResource替代DynamicResource,并且用程序来实现运行时对象属性的改变,例如实现theme变化功能。

  • 相关阅读:
    python 教程 第十七章、 网络编程
    SecureCRT循环检查设备状态
    《让僵冷的翅膀飞起来》系列之一——从实例谈OOP、工厂模式和重构
    设计,看上去很美!——“Design & Pattern”团队的第一块砖
    杂拌儿
    换了个计数器
    在Remoting客户端激活方式采用替换类以分离接口与实现
    动动手脚,protected不再被保护
    博客园出现在MSDN中国的首页链接
    近期写作计划和读书安排
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1659489.html
Copyright © 2011-2022 走看看