방법: 읽기 전용 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 개체 개요를 참조하십시오.