Comment : déterminer si un Freezable est gelé

Cet exemple montre comment déterminer si un Freezable objet est figé. Si vous essayez de modifier un objet figé Freezable , il lève un InvalidOperationException. Pour éviter de lever cette exception, utilisez la IsFrozen propriété de l’objet Freezable pour déterminer s’il est figé.

Exemple

L’exemple suivant fige un SolidColorBrush puis le teste à l’aide de la IsFrozen propriété pour déterminer s’il est figé.


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


Pour plus d’informations sur les objets, consultez la vue d’ensemble Freezabledes objets Freezable.

Voir aussi