未正确分析给定的XAML文件内容

本文关键字:XAML 文件 | 更新日期: 2023-09-27 18:29:13

我正在尝试使用XamlReader解析一个XML文件,该文件具有以下内容

    <Canvas xmlns:seic="clr-namespace:NameSpace1;assembly=Assembly1" 
    xmlns:seidm="clr-namespace:NameSpace2;assembly=Assembly2" 
    xmlns:seogptpi="clr-namespace:NameSpace3;assembly=Assembly3" 
    xmlns:seogdc="clr-NameSpace4;assembly=Assembly4" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Name="_99abfc59e3ad417d98db31591a6f9dd7" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    Canvas.Left="0" Canvas.Top="0" Tag="TemplatePart" Width="800" Height="600"  
    seogdc:FrameworkElementExtensions.Id="Canvas" 
    seogdc:FrameworkElementExtensions.ShowRotateCue="False" 
    seogdc:FrameworkElementExtensions.ShowPositionCue="False" 
    seogdc:FrameworkElementExtensions.IsSizeSerializable="False" 
    seogdc:FrameworkElementExtensions.IsTargetDimensions="False">
<Canvas x:Name="Canvas1" 
      Canvas.Left="0"    
      Canvas.Top="0" 
      Tag="TemplatePart" 
      Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Canvas}}, Path=Width}" 
      Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Canvas}}, Path=Height}" 
      Background="{x:Static Brushes.Black}"
      seogdc:FrameworkElementExtensions.Id="Canvas" 
      seogdc:FrameworkElementExtensions.ShowRotateCue="False" 
      seogdc:FrameworkElementExtensions.ShowPositionCue="False" 
      seogdc:FrameworkElementExtensions.IsTargetDimensions="False" 
      seogptpi:TemplateManager.IsMainPart="True"
      seogptpi:TemplateManager.TemplateName="Canvas">
<seic:DomainParameter.DomainModel>
  <seidm:StatelessDomainModel>
    <seidm:StatelessDomainModel.DomainFeatures>
      <seidm:ColorFeature LabelFillTransparency="0">
        <seidm:ColorFeature.FontBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.FontBlinkSetting>
        <seidm:ColorFeature.LineBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.LineBlinkSetting>
        <seidm:ColorFeature.FillBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.FillBlinkSetting>
      </seidm:ColorFeature>
    </seidm:StatelessDomainModel.DomainFeatures>
  </seidm:StatelessDomainModel>
</seic:DomainParameter.DomainModel>
  </Canvas>
</Canvas>

但它只是一直抛出XamlParseException("Cannot set unknown member"Namespace2.ColorFeature.LabelFillTransparency"。"行号为"13",行位置为"31"。)。我添加了对上述4个组件的引用。出于隐私目的,我选择不提供确切的程序集名称。有人能告诉我可能发生了什么吗?

提前谢谢。

未正确分析给定的XAML文件内容

您需要为XamlReader.Parse方法提供一个自定义的ParserContext。您应该分配ParserContext.XamlTypeMapper属性以使XamlParser能够定位XAML中引用的类型。大致如下:

var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(
  new[] {
    Assembly.GetExecutingAssembly().GetName().Name,
    "Assembly1",
    "Assembly2",
    "Assembly3",
    "Assembly4",
  }
);
var element = (FrameworkElement) XamlReader.Parse(xaml, context);