更新:2007 年 11 月
下列範例說明如何判斷 Freezable 物件是否凍結。如果您試著修改凍結的 Freezable 物件,它會擲回 InvalidOperationException。為避免擲回這個例外狀況,請使用 Freezable 物件的 IsFrozen 屬性,判斷物件是否凍結。
範例
下列範例會凍結 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;
}
如需 Freezable 物件的詳細資訊,請參閱 Freezable 物件概觀。