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变化功能。

  • 相关阅读:
    数据结构算法
    C++字符串完全指引之二 —— 字符串封装类
    gethostbyname根据主机名获得地址方法
    通过注册表来限定程序的使用时限
    查询图像上的匹配块
    VC++6.0中内存泄漏检测
    qsort 浅析
    shell 中数学计算总结
    最新感悟基础才是王道
    条件表达式中的匹配
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1659489.html
Copyright © 2011-2022 走看看