Compartir a través de


Cómo: Obtener una copia modificable de un elemento Freezable de sólo lectura

En este ejemplo se muestra cómo utilizar el método Clone para crear una copia para escritura de un objeto Freezablede sólo lectura.

Una vez que un objeto Freezable se marca como de sólo lectura ("inmovilizado"), no es posible modificarlo. Sin embargo, es posible utilizar el método Clone para crear un clon modificable del objeto inmovilizado.

Ejemplo

En el ejemplo siguiente se crea un clon modificable de un objeto SolidColorBrush inmovilizado.

            Dim myButton As New Button()
            Dim myBrush As New SolidColorBrush(Colors.Yellow)

            ' Freezing a Freezable before it provides
            ' performance improvements if you don't
            ' intend on modifying it. 
            If myBrush.CanFreeze Then
                ' Makes the brush unmodifiable.
                myBrush.Freeze()
            End If


            myButton.Background = myBrush

            ' If you need to modify a frozen brush,
            ' the Clone method can be used to
            ' create a modifiable copy.
            Dim myBrushClone As SolidColorBrush = myBrush.Clone()

            ' Changing myBrushClone does not change
            ' the color of myButton, because its
            ' background is still set by myBrush.
            myBrushClone.Color = Colors.Red

            ' Replacing myBrush with myBrushClone
            ' makes the button change to red.
            myButton.Background = myBrushClone
Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);

// Freezing a Freezable before it provides
// performance improvements if you don't
// intend on modifying it. 
if (myBrush.CanFreeze)
{
    // Makes the brush unmodifiable.
    myBrush.Freeze();
}


myButton.Background = myBrush;  

// If you need to modify a frozen brush,
// the Clone method can be used to
// create a modifiable copy.
SolidColorBrush myBrushClone = myBrush.Clone();

// Changing myBrushClone does not change
// the color of myButton, because its
// background is still set by myBrush.
myBrushClone.Color = Colors.Red;

// Replacing myBrush with myBrushClone
// makes the button change to red.
myButton.Background = myBrushClone;

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

Vea también

Referencia

Freezable

CloneCurrentValue

Conceptos

Información general sobre objetos Freezable

Otros recursos

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