Condividi tramite


Procedura: determinare se un oggetto Freezable è bloccato

In questo esempio viene illustrato come determinare se un oggetto Freezable è bloccato. Se si tenta di modificare un oggetto Freezable bloccato, viene generata un'eccezione InvalidOperationException. Per evitare di generare questa eccezione, utilizzare la proprietà IsFrozen dell'oggetto Freezable per determinare se è bloccato.

Esempio

Nell'esempio seguente viene bloccato un oggetto SolidColorBrush, che viene quindi testato utilizzando la proprietà IsFrozen per determinare se è bloccato.


            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



            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;
            }


Per ulteriori informazioni sugli oggetti Freezable, vedere Cenni preliminari sugli oggetti Freezable.

Vedere anche

Riferimenti

Freezable

IsFrozen

Concetti

Cenni preliminari sugli oggetti Freezable

Altre risorse

Procedure relative agli elementi di base