此範例示範如何藉由呼叫其 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 物件詳細資訊,請參閱 Freezable 物件概觀。