Nasıl yapılır: Freezable'ın Dondurulmuş Olup Olmadığını Belirleme

Bu örnekte bir nesnenin donmuş olup Freezable olmadığının nasıl belirlenip belirlenip belirlene olmadığı gösterir. Donmuş bir nesneyi değiştirmeye Freezable çalışsanız bir InvalidOperationException oluşturur. Bu özel durumun atılmasından kaçınmak IsFrozen için, donmuş olup Freezable olmadığını belirlemek için nesnesinin özelliğini kullanın.

Örnek

Aşağıdaki örnek bir'i SolidColorBrush dondurup dondurup dondurulup dondurulmaması IsFrozen için özelliğini kullanarak test ediyor.


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


Nesneler hakkında daha fazla Freezable bilgi için bkz. Freezable Objects Overview.

Ayrıca bkz.