当Style的TargetType未连接到调试器时会引发XamlParseException

12

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

<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

如果我注释掉两个Style节点,那么一切都正常了。有什么想法吗?


TargetType="{x:Type <type>}" 语法怎么样? - Jay
@Jay:同样的结果,不幸的是。 - Adam Robinson
你是否将自定义控件的样式放在Themes/Generic.xaml中? - Justin XL
嗯,Xaml 文件可以是任何东西,我不清楚你正在使用什么。 - Justin XL
1
@Xin:那些不是自定义控件,它们是DevExpress WPF工具集的一部分。我正在尝试在整个应用程序中应用默认样式到这两个对象。 - Adam Robinson
显示剩余3条评论
4个回答

29

我曾遇到相似的问题,解决方法是检查我的XAML样式资源文件是否全部设置了"Page"(在"建置操作"属性中)。默认情况下,它们并不都是此模式(有些是"Content",有些是"compile"或"embedded resource"或"resource"),这就会导致此类问题。

也许对你也有帮助...

编辑:据我所知,这与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>

但是对于其他选项,这种方法将无效(至少不是所有情况都有效,我没有尝试每一个选项),因为调用会在定义之前被解析。

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

编辑2:对我来说,这是一场噩梦般的调试,因为它甚至更糟糕:它在某些发布配置下工作,在其他情况下则不工作,因为根据用户执行的操作顺序,他可能会得到错误或不会得到错误(当调用时,资源是否已经在内存中加载,取决于此时WPF已经需要什么),当然我不能像平常那样“调试”,因为调试模式总是可以工作...在找到这个解决方案之前我花了一段时间...很高兴它有所帮助。


不错的呼叫!问题解决了。公平地说,我在赏金描述中确实提到了我想要一个关于为什么会这样的解释;你有什么想法吗? - Adam Robinson
看我的编辑,但是就像写的那样,没有证明什么,只是我推断出来的:) - David
1
谢谢,这与我的经验不太一致(我从未实际在发布模式下构建过,它总是在调试模式下编译,并且只有在调试器附加时才能工作,而不是在调试器分离时)。无论如何,这是我能找到的最好的解释,所以当计时器结束时(大约5分钟后),我会授予奖励。 - Adam Robinson
哇,那真是一个简单的修正。在测试之前,我很乐观地投了赞成票,结果它确实起作用了 :-) - Simon_Weaver
这对我也起作用了,谢谢。具有讽刺意味的是,我以前发现了非常相似的东西,当我读到你的答案时,感觉就像是“早已经历”。具体来说,当使用x:Shared时,Resource的构建操作也会引发XamlParseException。这里是connect bug,WPF团队关闭了它,因为他们不会解决它。 - Adam Caviness
显示剩余2条评论

4

对我来说,解决方法有些不同。我已经将构建操作设置为“页面”,但是我必须删除任何目录。例如:

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

将成为

   <ResourceDictionary Source="Common.xaml" />

值得注意的是,由于我需要与Silverlight共享文件,所以我被链接了文件。

1

你的项目中是否引用了带有app.xaml.cs的devexpress dlls?如果没有,请尝试添加引用。

同时,使用fuslogvw.exe检查程序集加载情况,并确保devexpress dlls已被加载。


我还没有尝试过你的第二步,但是是的,共同的项目(其中包含Xaml文件)和主要的项目(带有App.xaml)都引用了包含这些对象的DLL。 - Adam Robinson
+1 是个好主意,但是另一个答案解决了我的问题。谢谢! - Adam Robinson

0

我在运行应用程序时遇到了一个XamlParseException,错误信息为“无法从文本's:ScatterViewItem'创建'Type'”,但只有在直接运行应用程序时才会出现此问题,而在Visual Studio调试器中则没有。

这个外部答案告诉我,由于某种原因,必要的程序集尚未加载。这个StackOverflow答案向我展示了如何为我的WPF应用程序创建一个Main()函数。我只需在Main()的顶部放置“new ScatterViewItem();”,问题就解决了!

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接