本例演示如何通过调用其 Freezable 方法使 Freeze 成为只读。
如果以下任一与对象有关的条件为 Freezable,则无法冻结 true
对象:
如果 false
对象的这些条件为 Freezable,并且你不打算修改它,请考虑冻结它以获得性能优势。
示例:
以下示例冻结一个 SolidColorBrush,它是 Freezable 对象的一种类型。
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
myButton.Background = myBrush;
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
有关 Freezable 对象的详细信息,请参阅 冻结对象概述。