이 예제에서는 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 개체 개요를 참조하세요.
참고하십시오
- Freezable
- IsFrozen
- Freezable 개체 개요
- 사용법 주제
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback