Compartir a través de


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

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

Ejemplo

En el ejemplo siguiente se inmoviliza SolidColorBrush y, a continuación, se comprueba mediante la propiedad IsFrozen si se encuentra inmovilizado.


            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



            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;
            }


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

Vea también

Referencia

Freezable

IsFrozen

Conceptos

Información general sobre objetos Freezable

Otros recursos

Temas "Cómo..." sobre elementos base