XamlParseException throws a call to XamlReader.Load

I play with .net 4 System.Windows.Markup.XamlReader- as an exercise in education - and I continue to encounter the same problem: loading xaml with a XamlReader.Loadcalls a XamlParseExceptionif the root object determines x:Class, but successfully analyzes and loads the node if not.

Here is the code I'm trying:

using System.Windows;
using System.Xaml;
using XamlReader = System.Windows.Markup.XamlReader;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Load up UserControl1.xaml from the solution
            var reader = new XamlXmlReader(@"../../UserControl1.xaml", XamlReader.GetWpfSchemaContext());
            var userControl = XamlReader.Load(reader) as UserControl1;

            //  Do something with userControl...
        }
    }
}

I tried XamlReader.Parsedirectly from a line containing xaml with the same result: only works if the x: Class definition is not defined.

Deleting an ad x:Classdoesn't seem like a good option, because then I lose the code, especially the callInitalizeComponent()

Exclusion Details:

'Specified class name 'WpfApplication2.UserControl1' doesn't match actual root instance type 'System.Windows.Controls.UserControl'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.'

... but I do not know how (where) to install XamlObjectWriterSettings.RootObjectInstance(or really, if necessary?)

Any clues?

+3
2

XamlReader - , , . XAML, - , , XAML , XAML, (, ) .

+6

x: Class XAML. , XAML. , .

XAML XML ?

+5

Source: https://habr.com/ru/post/1796442/


All Articles