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

  • 相关阅读:
    Excel操作基本方法 服务器端不用安装Office工具
    使用IrisSkin2给软件"换肤"
    手机进行GPRS连接的代码(C#.NET开发)
    C# DataGridView 倒出word
    win2003优化大全 (转载)
    c# Invoke和BeginInvoke 区别
    关于sqlite3使用top的一些规则总结
    C#拷贝文件和复制文件夹实例代码 C#拷贝文件
    c# FileSystemWatcherc# FileSystemWatcher控件的使用方法控件的使用方法
    所见即所得富文本编辑器实现原理 转
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1659489.html
Copyright © 2011-2022 走看看