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

  • 相关阅读:
    android 21 隐式意图启动系统预定义activity
    android 20 Intnet类重要的成员变量
    android 19 activity纵横屏切换的数据保存与恢复
    android 18 Bundle类
    android 17 activity生命周期
    Centos6.4建立本地yum源
    eclipse实现批量修改文件的编码方式
    [系统资源]/proc/meminfo和free输出解释
    监控WebLogic 8.x的解决方案(监控应用服务器系列文章)
    linux网络监控脚本
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1659489.html
Copyright © 2011-2022 走看看