다음을 통해 공유


방법: Freezable의 고정 여부 확인

이 예제에서는 Freezable 개체가 고정되었는지 여부를 확인하는 방법을 보여 줍니다. 고정된 Freezable 개체를 수정하려고 하면 InvalidOperationException이 throw됩니다. 이 예외를 throw하지 않으려면 Freezable 개체의 IsFrozen 속성을 사용하여 고정 여부를 확인합니다.

예시

다음 예제에서는 SolidColorBrush를 고정한 다음, IsFrozen 속성을 통해 고정 여부를 확인하여 테스트합니다.


Button myButton = new Button();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);

if (myBrush.CanFreeze)
{
    // Makes the brush unmodifiable.
    myBrush.Freeze();
}

myButton.Background = myBrush;

if (myBrush.IsFrozen) // Evaluates to true.
{
    // If the brush is frozen, create a clone and
    // modify the clone.
    SolidColorBrush myBrushClone = myBrush.Clone();
    myBrushClone.Color = Colors.Red;
    myButton.Background = myBrushClone;
}
else
{
    // If the brush is not frozen,
    // it can be modified directly.
    myBrush.Color = Colors.Red;
}


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

If myBrush.CanFreeze Then
    ' Makes the brush unmodifiable.
    myBrush.Freeze()
End If

myButton.Background = myBrush


If myBrush.IsFrozen Then ' Evaluates to true.
    ' If the brush is frozen, create a clone and
    ' modify the clone.
    Dim myBrushClone As SolidColorBrush = myBrush.Clone()
    myBrushClone.Color = Colors.Red
    myButton.Background = myBrushClone
Else
    ' If the brush is not frozen,
    ' it can be modified directly.
    myBrush.Color = Colors.Red
End If


Freezable 개체에 관한 자세한 내용은 Freezable 개체 개요를 참조하세요.

참고하십시오