方法 : 読み取り専用の Freezable の書き込み可能なコピーを取得する

Clone メソッドを使用して読み取り専用の Freezable の書き込み可能なコピーを作成する方法を次の例に示します。

Freezable オブジェクトは、読み取り専用 ("固定") としてマークされた後には変更できません。 ただし、Clone メソッドを使用して、固定されたオブジェクトの変更可能な複製を作成できます。

使用例

次の例では、固定された SolidColorBrush オブジェクトの変更可能な複製を作成します。

            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;

Freezable オブジェクトの詳細については、「Freezable オブジェクトの概要」を参照してください。

参照

参照

Freezable

CloneCurrentValue

概念

Freezable オブジェクトの概要

その他の技術情報

基本要素に関する「方法」トピック