Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style TargetType causes XamlParseException when not attached to debugger

Tags:

wpf

xaml

.net-4.0

I have a very simple set of styles that I'm using in a couple of different WPF applications. I have this style stored in a Xaml file in a common project, then added by merging into the Resources in App.xaml in each of the projects.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
                    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    <Style TargetType="dxe:ComboBoxEdit">
        <Setter Property="AutoComplete" Value="True" />
        <Setter Property="IncrementalFiltering" Value="True" />
        <Setter Property="ImmediatePopup" Value="True" />
        <Setter Property="IsTextEditable" Value="True" />
        <Setter Property="ClearSelectionOnBackspace" Value="True" />
    </Style>
    <Style TargetType="dxe:ComboBoxEditSettings">
        <Setter Property="AutoComplete" Value="True" />
        <Setter Property="IncrementalFiltering" Value="True" />
        <Setter Property="ImmediatePopup" Value="True" />
        <Setter Property="IsTextEditable" Value="True" />
    </Style>
</ResourceDictionary>

Unfortunately, something about this is causing a XamlParseException regarding the TargetType property, but only when not attached to the debugger. If I start the application in the debugger, everything is fine. If I "Start Without Debugging", I get this as App.xaml is being loaded:

System.Windows.Markup.XamlParseException: 'Failed to create a 'TargetType' from the text 'dxe:ComboBoxEdit'.' Line number '5' and line position '12'. ---> System.Xaml.XamlParseException: Type reference cannot find type named '{http://schemas.devexpress.com/winfx/2008/xaml/editors}ComboBoxEdit'.
   at MS.Internal.Xaml.Context.ObjectWriterContext.ServiceProvider_Resolve(String qName)
   at MS.Internal.Xaml.ServiceProviderContext.System.Windows.Markup.IXamlTypeResolver.Resolve(String qName)
   at System.Xaml.Replacements.TypeTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateObjectWithTypeConverter(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateFromValue(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value, XamlMember property)
   at System.Xaml.XamlObjectWriter.Logic_CreateFromValue(ObjectWriterContext ctx, XamlValueConverter`1 typeConverter, Object value, XamlMember property, String targetName, IAddLineInfo lineInfo)
   --- End of inner exception stack trace ---
   at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Shell.App.InitializeComponent() in c:\DevProjects\CoreApplication\Shell\App.xaml:line 1
   at Shell.App.Main() in C:\DevProjects\CoreApplication\Shell\obj\x86\Debug\App.g.cs:line 0

If I comment out both Style nodes, then everything works correctly. Any ideas?

like image 741
Adam Robinson Avatar asked Nov 28 '11 17:11

Adam Robinson


1 Answers

I had the same kind of issue, and for me it was the way the resource files were added to the project.

I had to make sure each and every one of my xaml style resource files were set up as "Page" (in the "build action" property). By default, they were not all in this mode (some as "Content", other as "compile" or "embedded resource" or "resource") and that caused this kind of issue.

maybe it's the same for you...

edit : from what I could gather, it has something to do with how the xaml code is embedded in the project and specifically the Order in which it is parsed by WPF: if the source file is set as "Page", the order in which the dictionary are merged is not taken into account, so this would work in release mode:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- let's say this dictionary contains a call to "MyButtonStyle" ... -->
            <ResourceDictionary Source="resources/Common.xaml" />
            <!-- ... and this one contains the definition of "MyButtonStyle" -->
            <ResourceDictionary Source="resources/GeneralResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

but this would not work with the other options (not all of them anyway, I did not try every single one of them) since the call would be parsed before the definition.

As to why it works in debug mode and not in release (or in your case when you start without debug), I guessed it was because WPF parses and stores resources into memory differently depending on the mode you're in (debug or not). I think WPF stores everything in memory in debug mode while it only stores what it needs in release mode (so for instance the calling code and not the definition code), but then again, it's just my guess...

edit 2: for me it was a nightmare to debug because it was even worse: it worked in some release configurations and not others because depending on the order of the actions taken by the user, he would get the error or not (the resource might already have been charged in memory or not when called, depending on what WPf already needed at this point), and of course I could not "debug" per say, since the debug mode always worked... It took me a while before figuring this solution out... glad it helped

like image 84
David Avatar answered Sep 21 '22 21:09

David