如何解析具有自定义类的xaml文件,以获得所有对象的列表?
我浏览了大量类似的帖子,但在我的案例中似乎没有任何效果。我所要做的就是加载位于其他程序集中的xaml文件,并遍历不同的元素/对象以检查特定的属性值。 我本可以将其作为一个简单的XML文件来阅读,但诸如样式等内容无法通过XML解析来捕获。 在搜索了很多之后,我尝试了以下两件事: 我删除了x:Class=。。从xaml 我还添加了assembly=XBase.UI,因为我原来的xaml没有这个,我读到动态加载时,您需要知道如何指定程序集 然后,我将剩余的文件作为xml流加载。 然后我打电话给XamlReader.Loadstream 这似乎适用于发布查询的人,但我有一个例外如何解析具有自定义类的xaml文件,以获得所有对象的列表?,xaml,xamlparseexception,xamlreader,Xaml,Xamlparseexception,Xamlreader,我浏览了大量类似的帖子,但在我的案例中似乎没有任何效果。我所要做的就是加载位于其他程序集中的xaml文件,并遍历不同的元素/对象以检查特定的属性值。 我本可以将其作为一个简单的XML文件来阅读,但诸如样式等内容无法通过XML解析来捕获。 在搜索了很多之后,我尝试了以下两件事: 我删除了x:Class=。。从xaml 我还添加了assembly=XBase.UI,因为我原来的xaml没有这个,我读到动态加载时,您需要知道如何指定程序集 然后,我将剩余的文件作为xml流加载。 然后我打电话给Xaml
'System.Windows.Markup.XamlParseException : 'The invocation of the constructor on type 'XBase.UI.XControlBase' that matches the specified binding constraints threw an exception.' Line number '6' and line position '55'.
----> System.InvalidOperationException : The calling thread must be STA, because many UI components require this.'
我尝试的第二件事是使用XamlReader.Parse并提供ParserContext。
这就是我所做的:
var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[] { });
context.XamlTypeMapper.AddMappingProcessingInstruction("UI", "XBase.UI", ""); //assemblyname is empty as my original file doesn't have one
context.XmlnsDictionary.Add("UI", "clr-namespace:XBase.UI;assembly=XBase.UI");
string text = File.ReadAllText(xamlfile);
var object = XamlReader.Parse(xamlfile, context);
这也引发了一个异常:
'System.Windows.Markup.XamlParseException : 'Cannot create unknown type '{clr-namespace:XBase.UI}XControlBase'.' Line number '1' and line position '2'.
----> System.Xaml.XamlObjectWriterException : 'Cannot create unknown type '{clr-namespace:XBase.UI}XControlBase'.' Line number '1' and line position '2'.'
我的原始xaml文件
<UI:XControlBase x:Class="XBase.UI.XListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:UI="clr-namespace:XBase.UI" Height="Auto" Width="Auto" IsTabStop="False"
mc:Ignorable="d"
Visibility="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}">
<UI:XControlBase.Resources>
<UI:ButtonTemplateSelector x:Key="ButtonSelector" />
</UI:XControlBase.Resources>
<UI:ItemControlWrapper
ItemsSource="{Binding ButtonList}"
ItemTemplateSelector="{StaticResource ButtonSelector}"
IsTabStop="False">
<UI:ItemControlWrapper.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="{Binding ListOrientation}" />
</ItemsPanelTemplate>
</UI:ItemControlWrapper.ItemsPanel>
</UI:ItemControlWrapper>
</UI:XControlBase>
请帮帮我。我甚至不确定这是否是实现我想要的目标的正确方法。如果有另一种方法以更有意义的方式列出某些类型的所有元素,而不是
XmlDocument的GetElementsByTagName,请告诉我
提前谢谢 我现在也有这个问题。