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

  • 相关阅读:
    SAP-MM采购视图-利润中心在“工厂数据/存储2”
    SAP-物料主数据维护之(通用名称/商用名称维护)
    SAP BP维护客商主数据之(交货与付款条件)定义
    SAP MM01创建物料时配置价格
    SAP SD后台配置清单
    SAP- 物料主数据之多工厂设置
    SAP如何查询表名及字段
    SAP学习之:F-65制作财务客户收款凭证
    SAP中创建采购信息记录 (ME11)
    SAP DEV-Q-PRO系统环境默认配置规则
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1659489.html
Copyright © 2011-2022 走看看