How to parse xaml file having custom class,to get the list of all objects?

1.2k views Asked by At

I went through the tons of similar posts but nothing seems to work in my case. All I want to do is load a xaml file (located in some other assembly), and iterate through the different elements/objects to check for a certain attribute value. I could have read it as a simple XML file, but things like styles etc cannot be caught through xml parsing. After searching a lot,I tried the following two things:

  1. I removed the x:Class=".." from the xaml I also added the assembly=XBase.UI in this ( as my original xaml didn't have this and I read that when loading dynamically, you need to know specify the assembly) then I loaded the remaining file as xml stream. Then I called XamlReader.Load(stream)

This seems to work for people who posted queries, but I get exception

'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.'

The second thing I tried was to use the XamlReader.Parse and provide the ParserContext along with it. This is what I did:

   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);

This too throws an exception:

'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'.'

My original xaml file

<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>

Please help me out here. I am not even sure if this is the right way to achieve what I want. If there is another way to list all of the elements of certain kind in a more meaningful way other than the XmlDocument's GetElementsByTagName, please let me know.

Thanks in advance!

0

There are 0 answers