Specified value of type ‘System.Windows.Media.SolidColorBrush’ must have IsFrozen set to false to modify.

In quest’ ultimo anno ho lavorato ad un progetto WPF 4.0 molto grosso per un cliente.

Da alcuni mesi di tanto in tanto si presentava un’ eccezione con internal error questo:

Specified value of type 'System.Windows.Media.SolidColorBrush' must have IsFrozen set to false to modify.

e con stack trace:

   at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri)
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
   at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
   at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
   at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
   at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
   at System.Windows.FrameworkElement.ApplyTemplate()
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

Si trovano pochissime informazioni a riguardo, dopo varie ricerche ho trovato questa segnalazione di bug a Microsoft:

WPF 4.0 Freezable Bug ! WritePreamble exception while loading a UI element.

All’ interno sono ben spiegate tutte le casistiche e sono presenti due soluzioni

More info may be help:
(1)if not use the SolidColorBrush in DrawingBrush resource, no problem!
(2)if force to freeze the SolidColorBrush at first time,no problem too!

Considerando la prima soluzione una cosa impensabile su un grosso progetto, ho tentato la seconda strada su tutti i SolidColorBrush definiti.

Ecco un esempio su come forzare che il SolidColorBrush sia freeze

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<SolidColorBrush x:Key="SemiTransparentDarkBlueGreenBrushKey" Color="#FF004451" po:Freeze="True"/>
<SolidColorBrush x:Key="BackgroundInterno1Livello" Color="#FF1B5762" po:Freeze="True"/>
<SolidColorBrush x:Key="BackgroundInterno2Livello" Color="#FF326872" po:Freeze="True"/>
<SolidColorBrush x:Key="BackgroundInternoHeader" Color="#FF004450" po:Freeze="True"/>
<SolidColorBrush x:Key="BackgroundWhite" Color="#4CFFFFFF" po:Freeze="True"/>
</ResourceDictionary>

riporto anche questo link:
Freezable Objects Overview

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in .Net, c#, WPF e contrassegnata con , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...