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

时间:2011-11-28 17:18:29

标签: wpf xaml .net-4.0

我在一些不同的WPF应用程序中使用了一组非常简单的样式。我将此样式存储在一个公共项目的Xaml文件中,然后通过合并到每个项目中Resources的{​​{1}}中添加。

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> 关于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 个节点,那么一切正常。有什么想法吗?

4 个答案:

答案 0 :(得分:28)

我遇到了同样的问题,对我而言,这是资源文件添加到项目中的方式。

我必须确保我的xaml样式资源文件中的每一个都设置为“Page”(在“构建操作”属性中)。默认情况下,它们不是全部处于此模式(某些为“内容”,其他为“编译”或“嵌入资源”或“资源”),而 导致此类问题。

也许对你来说也一样......

编辑从我可以收集到的内容中,它与xaml代码嵌入项目中的方式有​​关,特别是与 Order 有关的解析方式。 WPF:如果源文件设置为“页面”,则不会考虑合并字典的顺序,因此这将在发布模式下工作:

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

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

至于为什么它在调试模式下而不是在发布模式下工作(或者在没有调试时启动的情况下),我猜测这是因为WPF根据你所处的模式不同地解析和存储资源到内存中(调试或不)。我认为WPF在调试模式下将所有内容存储在内存中,而它只在发布模式下存储它所需的内容(例如调用代码而不是定义代码),但是再次,它只是我的猜测......

编辑2:对我来说这是调试的噩梦,因为它更糟糕:它在一些发布配置中工作而不是其他配置,因为根据用户采取的操作顺序,他会得到错误(在调用时,资源可能已经在内存中充电,这取决于此时WPf已经需要的内容),当然我无法按照说法“调试”,因为调试模式始终有效...它花了我一个在确定这个解决方案之前......很高兴帮助

答案 1 :(得分:4)

对我而言,有些不同之处。我已经将构建操作设置为'Page',但我必须删除任何目录。例如:

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

会变成

   <ResourceDictionary Source="Common.xaml" />

值得注意的是,自从我与silverlight共享文件后,我就成了链接文件

答案 2 :(得分:1)

你有没有参考app.xaml.cs项目中的devexpress dll?如果没有尝试添加参考..

还要使用。检查装配加载 fuslogvw.exe并确保加载了devexpress dll。

答案 3 :(得分:0)

我收到一条XamlParseException,其中包含“无法从文本中创建'Type'的消息:ScatterViewItem'”,但仅在直接运行应用程序时,而不是从Visual Studio调试器运行。

This external answer告诉我,无论出于何种原因,尚未装载必要的装配。 This StackOverflow answer向我展示了如何为我的WPF应用程序创建Main()函数。我只是简单地说“新ScatterViewItem();”在我的Main()的顶部,问题消失了!

我不知道这里发生了什么,特别是因为ScatterViewItem被来自同一程序集的ScatterView包围。它确实听起来像XAML解析中的一个讨厌的错误。

相关问题