Freezable个派生类

Freezable 不能。

CanFreeze 属性的值,以确定是否可以冻结该对象,然后再尝试将其冻结。

如果满足以下任一条件,则无法冻结:

  • 它具有动画或数据绑定属性。


  • Freezable 子对象。

Freezable,则应将其冻结,以获取之前所述的性能优势。

CloneCurrentValue 方法创建未冻结的克隆。

Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);

if (myBrush.CanFreeze)
{
    // Makes the brush unmodifiable.
    myBrush.Freeze();
}

myButton.Background = myBrush;

if (myBrush.IsFrozen) // Evaluates to true.
{
    // If the brush is frozen, create a clone and
    // modify the clone.
    SolidColorBrush myBrushClone = myBrush.Clone();
    myBrushClone.Color = Colors.Red;
    myButton.Background = myBrushClone;
}
else
{
    // If the brush is not frozen,
    // it can be modified directly.
    myBrush.Color = Colors.Red;
}

然后,将使用它来设置按钮的背景。

xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
mc:Ignorable="PresentationOptions"
<
Page.Resources> <!-- This resource is frozen. --> <SolidColorBrush x:Key="MyBrush" PresentationOptions:Freeze="True" Color="Red" /> </Page.Resources>

由于并非所有 XAML 读取器都能识别此特性,因此建议使用mc:可忽略属性将 Presentation:Freeze 特性标记为可忽略

 

执行已注册以侦听Freezable对象Changed事件的对象的清理时,在释放该对象之前必须删除该委托。

Brush myBrush = new SolidColorBrush(Colors.Red);
Rectangle myRectangle = new Rectangle();
myRectangle.Fill = myBrush;

这意味着以下代码实际上不会使 myRect 可以进行垃圾回收:

myRectangle = null;

在这种情况下myBrush,仍然保持myRectangle活动状态,并在触发事件Changed时将其回

清除这些类型的对象的推荐方法是从BrushFill属性中删除 ,这反过来将删除Changed事件处理程序。

myRectangle.Fill = null;
myRectangle = null;

 

相关文章:

猜你喜欢
  • 2022-02-08
  • 2021-08-18
  • 2022-12-23
相关资源
相似解决方案