Style TargetType causes XamlParseException when not attached to debugger

前端 未结 4 1091
灰色年华
灰色年华 2021-02-07 20:44

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

相关标签:
4条回答
  • 2021-02-07 21:12

    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

    0 讨论(0)
  • 2021-02-07 21:12

    I was getting a XamlParseException with the message "Failed to create a 'Type' from the text 's:ScatterViewItem'", but only when running the application directly, not from the Visual Studio debugger.

    This external answer told me that, for whatever reason, the necessary assembly hadn't been loaded yet. This StackOverflow answer showed me how to create a Main() function for my WPF app. I simply put "new ScatterViewItem();" at the top of my Main(), and the problem went away!

    I have no idea what's going on here, especially since the ScatterViewItem is enclosed by a ScatterView from the same assembly. It sure sounds like a nasty bug in XAML parsing.

    0 讨论(0)
  • 2021-02-07 21:13

    For me what worked was a little different. I did already set the build action to 'Page', but any directories I had to remove. Example:

       <ResourceDictionary Source="resources/Common.xaml" />
    

    would become

       <ResourceDictionary Source="Common.xaml" />
    

    Of note, I was linked files since I was sharing them with silverlight

    0 讨论(0)
  • 2021-02-07 21:30

    Do you have reference to devexpress dlls in the project which has app.xaml.cs ? if not try by adding reference..

    Also check the assembly loading by using the fuslogvw.exe and make sure the devexpress dlls are loaded.

    0 讨论(0)
提交回复
热议问题