2011-11-28 26 views
12

我有一组非常简单的样式,我在几个不同的WPF应用程序中使用样式。我将这种风格存储在一个通用项目的Xaml文件中,然后通过合并到ResourcesApp.xaml中添加到每个项目中。样式TargetType在未连接到调试器时导致XamlParseException

<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节点,则一切正常。有任何想法吗?

+0

对于'TargetType =“{x:Type }”语法怎么样? – Jay

+0

@Jay:不幸的是,结果相同。 –

+0

您是否将您的自定义控件的样式放入Themes/Generic.xaml中? –

回答

28

我有同样的问题,对我来说,这是资源文件添加到项目的方式。

我不得不确保我的xaml样式资源文件中的每一个都被设置为“Page”(在“build action”属性中)。默认情况下,它们并不是全部处于这种模式(有些称为“内容”,也就是“编译”或“嵌入的资源”或“资源”),并且引起这种问题。

或许这对你一样...

编辑:从我可以收集,它有事情做与XAML代码是如何嵌入到项目和具体的订单其中它是由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在这一点上已经需要什么),当然由于调试模式一直工作,所以我不能“调试”每一个说法......我花了一段时间才发现这一点解决了......很高兴帮助

+0

好的电话!这解决了这个问题。然而,公平地说,我在赏金描述中表明,我想解释为什么*就是这样;有任何想法吗? –

+0

看到我的编辑,但作为书面,没有任何证明,这只是我推断:) – David

+1

谢谢;它不符合我的经验(我从来没有真正在发布模式下构建,它始终以调试模式编译,并且可以与调试器*附加*一起使用,但不能与它分离*)。在任何情况下,这都是我能够找到的解释,所以我会在定时器(从现在开始大约5分钟) –

1

你有没有参考devexpress dll的proj哪些有app.xaml.cs?如果不尝试添加参考..

还检查程序集加载通过使用 fuslogvw.exe并确保devexpress dll被加载。

+0

我还没有尝试过你的第二步,但是,通用项目(其中包含Xaml文件)和主项目(包含App.xaml)都包含对包含这些对象的DLL的引用。 –

+0

+1是一个好主意,但另一个答案解决了我的问题。谢谢! –

4

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

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

将成为

<ResourceDictionary Source="Common.xaml" /> 

值得注意的是,我被链接的文件,因为我是在Silverlight

0

我正在与消息XamlParseException“无法创建一个“共享它们在文本中键入'ScatterViewItem'“,但只能在直接运行应用程序时,而不是从Visual Studio调试器中运行。

This external answer告诉我,无论什么原因,必要的程序集尚未加载。 This StackOverflow answer向我展示了如何为我的WPF应用程序创建Main()函数。我只是把“新的ScatterViewItem();”在我的Main()的顶部,问题就消失了!

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

相关问题