Compartir a través de


Cómo: Determinar si un elemento Freezable está inmovilizado

En este ejemplo se muestra cómo saber si un objeto Freezable está inmovilizado. Si intenta modificar un objeto Freezable inmovilizado, se produce una excepción InvalidOperationException. Para evitarlo, use la propiedad IsFrozen del objeto Freezable para saber si está inmovilizado.

Ejemplo

En el ejemplo siguiente se inmoviliza un objeto SolidColorBrush y luego se comprueba mediante la propiedad IsFrozen para saber si está inmovilizado.


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


Para obtener más información sobre los objetos Freezable, vea Información general sobre objetos Freezable.

Vea también