Condividi tramite


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

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

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

Esempio

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

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

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

Vedi anche