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.Load
calls a XamlParseException
if 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
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var reader = new XamlXmlReader(@"../../UserControl1.xaml", XamlReader.GetWpfSchemaContext());
var userControl = XamlReader.Load(reader) as UserControl1;
}
}
}
I tried XamlReader.Parse
directly from a line containing xaml with the same result: only works if the x: Class definition is not defined.
Deleting an ad x:Class
doesn'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?