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

本文关键字:XamlParseException 应用程序 WPF 是什么 | 更新日期: 2023-09-27 18:33:13

更新:控件的加载。资源.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 实例化超过 100 万次的代码,而只显示内存不足异常 - 看不到E_NOINTERFACE。哈伦夫。

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

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

相关文章:
  • 没有找到相关文章