共用方式為


作法:製作可以冷凍的 Read-Only

此範例示範如何藉由呼叫其 Freezable 方法將 Freeze 設為唯讀。

如果下列任一 Freezable 條件與 true 對象有關,則您無法凍結 物件:

  • 該物件具備動畫或資料系結屬性。

  • 它有動態資源所設定的屬性。 如需動態資源的詳細資訊,請參閱 XAML 資源

  • 它包含 Freezable 無法凍結的子物件。

如果這些條件 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 物件概觀

另請參閱