Condividi tramite


Procedura: ottenere una copia scrivibile di un oggetto Freezable di sola lettura

In questo esempio viene illustrato come utilizzare il metodo Clone per creare una copia scrivibile di un oggetto Freezable di sola lettura.

Dopo che un oggetto Freezable viene contrassegnato come di sola lettura (bloccato), non è possibile modificarlo. Tuttavia, è possibile utilizzare il metodo Clone per creare un duplicato modificabile dell'oggetto bloccato.

Esempio

Nell'esempio seguente viene creato un duplicato modificabile di un oggetto SolidColorBrush bloccato.

            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;

Per ulteriori informazioni sugli oggetti Freezable, vedere Cenni preliminari sugli oggetti Freezable.

Vedere anche

Riferimenti

Freezable

CloneCurrentValue

Concetti

Cenni preliminari sugli oggetti Freezable

Altre risorse

Procedure relative agli elementi di base