簡體   English   中英

是什么在WPF應用程序中導致XamlParseException?

[英]What is causing XamlParseException in WPF application?

更新:在問題點之后(在兩個應用程序中)都發生了control.Resources.dll的加載,因此很可能是紅色鯡魚。

我有兩個應用程序,一個運行良好,一個不會出現各種異常。 它們都是WPF應用程序,都在.NET 3.5和Windows7下運行。 它們都建立在同一組引用/依賴關系上。 它們基本上是相同的程序。 我已經手動比較了它們的主要文件,以尋找問題的根源。

在啟動過程中會出現問題。 異常始終是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。

我遇到了同樣的問題,因此我發布了對我有用的解決方案。 我使用第三方DLL在WPF應用程序中創建用戶控件。 當我直接啟動我的應用程序時,它可以正常工作-但是,當我從另一個應用程序中啟動它(這是正常用法)時,它卻失敗了,並出現了與上述完全相同的問題。 為了解決它,我將用戶控件的變量聲明從XAML移到了后面的代碼中。 這意味着在創建變量並將其實例化時,現在已加載相同的DLL,因此沒有沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM