是什么导致WPF应用程序中的XamlParseException?

时间:2014-12-04 18:41:09

标签: c# .net wpf xaml xamlparseexception

更新:在故障点之后(在两个应用程序中)都会加载controls.Resources.dll,因此很可能是一个红色的鲱鱼。

我有两个应用程序,一个工作正常,一个不会以各种异常开始。它们是在.NET 3.5和Windows7下运行的WPF应用程序。它们都构建在同一组引用/依赖项上。它们基本上是相同的程序。我手动比较他们搜索问题源的主要文件无济于事。

启动期间出现问题。异常始终是XamlParseException,但原因(从SQL异常到多触摸异常)不同。直接堆栈是:

Unable to cast object of type 'System.Windows.Controls.ListView' to type 'Lightning.Controls.Configuration.MaterialsImportExportControl'.  

Error at object 'System.Windows.Controls.ListView' in markup file 'Lightning.UI.Controls;component/configuration/materials/xaml/materialsimportexportcontrol.xaml'. 
---> System.InvalidCastException:
Unable to cast object of type 'System.Windows.Controls.ListView' 
to type 'Lightning.Controls.Configuration.MaterialsImportExportControl'.
   at Lightning.Controls.Configuration.MaterialsImportExportControl.System.Windows.Markup.IComponentConnector.Connect(Int32 connectionId, Object target)
   at System.Windows.Markup.BamlRecordReader.ReadConnectionId(BamlConnectionIdRecord bamlConnectionIdRecord)
   at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
   --- End of inner exception stack trace ---
   at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
   at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
   at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
   at System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord, Object& element, ReaderFlags& flags, Type& delayCreatedType, Int16& delayCreatedTypeId)
   at System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
   at System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
   at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
   at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
   at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
   at System.Windows.Markup.TreeBuilder.Parse()
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at EndeavorUI.EndeavorPartIdWindow.InitializeComponent()
   at EndeavorUI.EndeavorPartIdWindow..ctor()
   at EndeavorUI.SystemBootUpWindow.WaitToCollapseWindow(Object sender, RoutedEventArgs e)

根据我在这里和其他地方的阅读,这可能实际上是由资源DLL未正确加载或资源冲突引起的。

我运行了Process Explorer,并观察到在工作程序中,在.NET程序集选项卡中,Controls.resources.dll文件有多个条目,这些条目在非工作程序中不存在。

我认为这些dll也应该加载到非工作程序中;有人会对他们为什么不这样做有任何想法吗?

编辑添加:这是它正在尝试处理的xaml文件的编辑版本:

    <controls:UserControlMultiTouchEnabled x:Class="Lightning.Controls.Configuration.MaterialsImportExportControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:configuration="clr-namespace:Lightning.Controls.Configuration"
        xmlns:KB="clr-namespace:Lightning.UI.Controls.KeyBoard"
        xmlns:controls="clr-namespace:Lightning.Controls"
    Loaded="LoadedHandler">
        <controls:UserControlMultiTouchEnabled.Resources>
            <controls:ConverterBooleanInverter x:Key="converterBooleanInverter" />
            <controls:MultiConverterObjectReferenceToVisibilityAndBooleanGeneralized x:Key="converterObjectRefAndBooleanToVisibility" />
            <controls:MultiConverterObjectReferenceToVisibilityAndBooleanGeneralized InvertBooleans="True" x:Key="converterObjectRefAndBooleanToVisibilityInvertBooleans" />
            <controls:ConverterObjectReferenceToVisibilityGeneralized x:Key="converterObjectRefToVisibility" />
            <controls:ConverterObjectReferenceToVisibilityGeneralized Invert="True" x:Key="converterObjectRefToVisibilityInverted" />
            <controls:ConverterBooleanToVisibilityGeneralized x:Key="converterBoolToVisibility" />
            <controls:ConverterBooleanToVisibilityGeneralized Invert="True" x:Key="converterBoolToVisibilityInverted" />
            <controls:ConverterBooleanToSelectionModeGeneralized x:Key="converterSelectionMode" />
            <controls:MultiConverterImportedMaterialDisplayInfoPropertiesToBooleanEnabled x:Key="enabledConverter" />
            <controls:MultiConverterImportedMaterialDisplayInfoPropertiesToVisibility x:Key="converterIMDIVisibility" />
            <controls:MultiConverterImportedMaterialDisplayInfoPropertiesToVisibility Invert="True" x:Key="converterIMDIVisibilityInverted" />
            <sys:String x:Key="LocStrings">Configuration.Materials.resx.MaterialsInternationalizedStrings</sys:String>
            <sys:String x:Key="AssemblyName">Lightning.UI.Controls</sys:String>

            <Style TargetType="{x:Type TextBox}">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" Value="0.5"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

            <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource {x:Type GroupBox}}">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" Value="0.5"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

            <Style TargetType="{x:Type Label}">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" Value="0.5"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

            <LinearGradientBrush x:Key="SelectedBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFE1E0E8" Offset="0"/>
                <GradientStop Color="#FFA2A4A4" Offset="1"/>
                <GradientStop Color="#FF797A7D" Offset="0.521"/>
            </LinearGradientBrush>
        </controls:UserControlMultiTouchEnabled.Resources>

        <StackPanel Orientation="Vertical">
            <GroupBox Header="{Localize GroupBoxLabelImportMaterials, {StaticResource AssemblyName}, {StaticResource LocStrings}, DefaultValue=Import Materials}">
                <ListView VerticalAlignment="Top"
                      Visibility="{Binding Path=ImportedMaterials, Converter={StaticResource converterObjectRefToVisibility}}"
                      MouseMove="MouseMoveHandler"
                      StylusDown="StylusDownHandler"
                      ScrollViewer.CanContentScroll="True"
                      MaxHeight="450"
                      ItemsSource="{Binding ImportedMaterials}" 
                      SelectedItem="{Binding CurrentImportedMaterialSelection}"
                Margin="4,2">
                    <ListView.View>
(removed code)
                    </ListView.View>
                </ListView>
            </GroupBox>
        </StackPanel>
    </controls:UserControlMultiTouchEnabled>

=================================

更新(怎么回事):

现在我已经发现如何在我的开发计算机上可靠地导致异常,这是我收到的例外情况:

System.Windows.Markup.XamlParseException occurred
  Message="Cannot create instance of 'JobDockControlH' defined in assembly 'cutterUIControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error at object 'System.Windows.Controls.Grid' in markup file 'cutterUIControls;component/jobqueueviewh/xaml/jobstagingcontrolh.xaml' Line 94 Position 22."
  Source="PresentationFramework"
  LineNumber=94
  LinePosition=22
  NameContext="slidingDockBorder"
  StackTrace:
       at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
  InnerException: System.Reflection.TargetInvocationException
       Message="Exception has been thrown by the target of an invocation."
       Source="mscorlib"
       StackTrace:
            at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
            at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
            at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
            at System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
       InnerException: System.TypeInitializationException
            Message="The type initializer for 'Lightning.JobReviewer.JobReviewViewModel' threw an exception."
            Source="Lightning.UI.Controls"
            TypeName="Lightning.JobReviewer.JobReviewViewModel"
            StackTrace:
                 at Lightning.JobReviewer.JobReviewViewModel.get_Instance()
                 at JobQueueViewH.JobDockViewModelH..ctor() in C:\Development\RonAchin\Dev\Z12.0\Endeavor\cutterUIControls\JobQueueViewH\cs\JobDockViewModelH.cs:line 239
                 at JobQueueViewH.JobDockControlH..ctor() in C:\Development\RonAchin\Dev\Z12.0\Endeavor\cutterUIControls\JobQueueViewH\XAML\JobDockControlH.xaml.cs:line 142
            InnerException: System.InvalidCastException
                 Message="Unable to cast COM object of type 'Windows7.Multitouch.ManipulationInterop.ManipulationProcessor' to interface type 'Windows7.Multitouch.ManipulationInterop.IManipulationProcessor'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A22AC519-8300-48A0-BEF4-F1BE8737DBA4}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
                 Source="Windows7.Multitouch"
                 StackTrace:
                      at Windows7.Multitouch.ManipulationInterop.ManipulationProcessor.set_SupportedManipulations(MANIPULATION_PROCESSOR_MANIPULATIONS value)
                      at Windows7.Multitouch.Manipulation.ManipulationProcessor.set_SupportedManipulations(ProcessorManipulations value) in C:\D\Visual Studio 2008\Projects\Lightning\Dev\Zx\References\Windows7Multitouch\Mutitouch\Windows7.Multitouch\ManipulationProcessor.cs:line 461
                      at Windows7.Multitouch.Manipulation.ManipulationProcessor..ctor(ProcessorManipulations supportedManipulations) in C:\D\Visual Studio 2008\Projects\Lightning\Dev\Zx\References\Windows7Multitouch\Mutitouch\Windows7.Multitouch\ManipulationProcessor.cs:line 402
                      at Lightning.JobReviewer.JobReviewViewModel..ctor() in C:\Development\RonAchin\Dev\Z12.0\Endeavor\Controls\JobReviewer\cs\JobReviewViewModel.cs:line 93
                      at Lightning.JobReviewer.JobReviewViewModel..cctor() in C:\Development\RonAchin\Dev\Z12.0\Endeavor\Controls\JobReviewer\cs\JobReviewViewModel.cs:line 136
                 InnerException:

奇怪的是,我可以在程序早期的循环中执行导致ManipulationProcessor实例化超过一百万次的代码,只显示一个内存不足的异常 - 看不到E_NOINTERFACE。 Harumph。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,因此我发布了适用于我的解决方案。我使用第三方DLL在WPF应用程序中创建用户控件。当我直接启动我的应用程序时,它可以正常工作-但是,当我从另一个应用程序中启动它(这是正常用法)时,它失败了,并出现了与上述完全相同的问题。 为了解决它,我将用户控件的变量声明从XAML移到了后面的代码中。这意味着在创建变量并将其实例化时,现在已加载了相同的DLL,因此没有冲突。