如何:使 Freezable 成为只读

本示例演示如何通过调用其 Freeze 方法使 Freezable 变为只读。

如果以下任何一个对象相关条件为 true,则无法冻结 Freezable 对象:

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

  • 它有由动态资源设置的属性。 有关动态资源的更多信息,请参见资源概述

  • 它包含无法冻结的 Freezable 子对象。

如果这些条件对于 Freezable 对象为 false,并且您不希望修改它,请将其冻结以提高性能。

示例

下面的示例冻结 SolidColorBrush(它属于 Freezable 对象类型)。

            Dim myButton As New Button()
            Dim myBrush As New SolidColorBrush(Colors.Yellow)

            If myBrush.CanFreeze Then
                ' Makes the brush unmodifiable.
                myBrush.Freeze()
            End If

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

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

myButton.Background = myBrush;  

有关 Freezable 对象的更多信息,请参见Freezable 对象概述

请参见

参考

Freezable

CanFreeze

Freeze

概念

Freezable 对象概述

其他资源

基元素帮助主题