次の方法で共有


方法: Freezable が固定されているかどうかを判別する

この例では、Freezable オブジェクトが固定されているかどうかを判別する方法を示します。 固定された Freezable オブジェクトを変更しようとすると、InvalidOperationException がスローされます。 この例外がスローされないようにするには、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 オブジェクトの概要」を参照してください。

関連項目