HOW TO:建立 Freezable 唯讀
本範例說明如何呼叫 Freezable 的 Freeze 方法,將它設為唯讀。
若 Freezable 物件的下列任一條件為 true,就不能凍結該物件:
如果 Freezable 物件的上述條件都為 false,而您也不打算修改它,可以考慮予以凍結來提升效能。
範例
下列範例會凍結屬於 Freezable 物件類型的 SolidColorBrush。
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 物件概觀。