未附加到调试器时,样式TargetType会导致XamlParseException

Ada*_*son 12 wpf xaml .net-4.0

我有一组非常简单的样式,我在几个不同的WPF应用程序中使用.我将这种样式存储在一个普通项目的Xaml文件中,然后通过合并到每个项目的Resourcesin App.xaml中来添加.

<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>
Run Code Online (Sandbox Code Playgroud)

不幸的是,关于这个问题引起了XamlParseException关于TargetType属性的问题,但是只有在未附加到调试器时才会出现.如果我在调试器中启动应用程序,一切都很好.如果我"开始没有调试",我得到这个App.xaml正在加载:

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
Run Code Online (Sandbox Code Playgroud)

如果我注释掉两个Style节点,那么一切正常.有任何想法吗?

Dav*_*vid 28

我有同样的问题,对我而言,资源文件被添加到项目中的方式.

我必须确保我的每个 xaml样式资源文件都设置为"页面"(在"构建操作"属性中).默认情况下,他们不都是在这种模式下(如一些"内容",另一个为"编译"或"嵌入的资源"或"资源"),并认为造成这种问题.

也许这对你来说是一样的......

编辑:从我可以收集的内容来看,它与xaml代码嵌入到项目中的方式有​​关,特别是WPF解析它的顺序:如果源文件设置为"Page",则顺序合并的字典没有考虑在内,所以这将在发布模式下工作:

<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>
Run Code Online (Sandbox Code Playgroud)

但这不适用于其他选项(不管怎么说都没有,我没有尝试过每一个选项)因为调用会在定义之前进行解析.

至于为什么它在调试模式下而不是在发布模式下工作(或者在没有调试时启动的情况下),我猜测这是因为WPF根据您所处的模式(不管是否调试)将资源分析并存储到内存中.我认为WPF在调试模式下将所有内容存储在内存中,而它只在发布模式下存储它所需的内容(例如调用代码而不是定义代码),但话又说回来,这只是我猜...

编辑2:对我来说这是一个调试的噩梦,因为它更糟糕:它在一些发布配置中工作而不是其他配置,因为根据用户采取的操作的顺序,他会得到错误(资源可能已经被调用内存或不被调用,取决于此时WPf已经需要的内容),当然我无法"调试"每个说法,因为调试模式总是有效...我花了一段时间才算出来这个解决方案......很高兴它有所帮助