如何:決定 Freezable 是否凍結
此範例會顯示如何確定 Freezable 物件是否已凍結。 如果您嘗試修改已凍結的 Freezable 物件,則會擲回 InvalidOperationException。 若要避免擲回此例外狀況,請使用 IsFrozen 物件的 Freezable 屬性來確定它是否已凍結。
範例
下列範例會凍結 SolidColorBrush,然後使用 IsFrozen 屬性來確定它是否已凍結,以測試它。
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
if (myBrush.CanFreeze)
{
// Makes the brush unmodifiable.
myBrush.Freeze();
}
myButton.Background = myBrush;
if (myBrush.IsFrozen) // Evaluates to true.
{
// If the brush is frozen, create a clone and
// modify the clone.
SolidColorBrush myBrushClone = myBrush.Clone();
myBrushClone.Color = Colors.Red;
myButton.Background = myBrushClone;
}
else
{
// If the brush is not frozen,
// it can be modified directly.
myBrush.Color = Colors.Red;
}
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
If myBrush.IsFrozen Then ' Evaluates to true.
' If the brush is frozen, create a clone and
' modify the clone.
Dim myBrushClone As SolidColorBrush = myBrush.Clone()
myBrushClone.Color = Colors.Red
myButton.Background = myBrushClone
Else
' If the brush is not frozen,
' it can be modified directly.
myBrush.Color = Colors.Red
End If
如需有關 Freezable 物件詳細資訊,請參閱 Freezable 物件概觀。