locked
XamlServices.Load fails with any WPF element

    שאלה

  • I'm just trying to load a WPF element described within an XAML document (like a Page, UserControl, etc) by opening the file thusly:

    var page = XamlServices.Load("c:\\MyPage.xaml");

    This throws an XamlObjectWriterException with the message 

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

    Now, it might be late friday, but FFS I used to be able to do this with XamlReader.Load!  

    Now what do I do?!?!?!?  

    ROB! HELP ME!

    Edit:
    System.Windows.Markup.XamlReader also fails with the same error.

    Edit2:
    Hmm.... its starting to make more sense to me.  Without the class definition, how can you actually deserialize the object?  Removing the class attribute fixes the exception

                var doc = XDocument.Load(fileName);
                var name = XName.Get("{http://schemas.microsoft.com/winfx/2006/xaml}Class");
                doc.Root.Attributes(name).Remove();
    יום שישי 04 דצמבר 2009 20:42

תשובות

  • First, you should always use System.Windows.Markup.XamlReader.Load to load WPF XAML.  XamlServices.Load will not work on all WPF XAML.


    You can't use XamlReader.Load or XamlServices.Load on a XAML file that has x:Class directly.  Try using Application.LoadComponent and point it to the XAML file and also to the object that you're using (Omfg.WTHLol.MyPage BTW, great name). 


    Why are you trying to load a XAML document that has an x:Class with it in the first place?  Why not just call the constructor of the class which should call InitailizeComponent which calls XamlReader.Load eventually?

    • סומן כתשובה על-ידי Will Sullivan יום שישי 11 דצמבר 2009 15:15
    יום חמישי 10 דצמבר 2009 20:29

כל התגובות

  • First, you should always use System.Windows.Markup.XamlReader.Load to load WPF XAML.  XamlServices.Load will not work on all WPF XAML.


    You can't use XamlReader.Load or XamlServices.Load on a XAML file that has x:Class directly.  Try using Application.LoadComponent and point it to the XAML file and also to the object that you're using (Omfg.WTHLol.MyPage BTW, great name). 


    Why are you trying to load a XAML document that has an x:Class with it in the first place?  Why not just call the constructor of the class which should call InitailizeComponent which calls XamlReader.Load eventually?

    • סומן כתשובה על-ידי Will Sullivan יום שישי 11 דצמבר 2009 15:15
    יום חמישי 10 דצמבר 2009 20:29
  • Thanks for the clarification.

    Why am I doing it?  I'm using DTE to scan the current instance of VS from within a Workflow 4.0 custom activity designer to find Pages and UserControls which users will designate as a template to be used within the workflow.  I need to spin up these templates in order to take a snapshot of them for use in the designer UI.

    I eventually used the x:Class attribute to search the current AppDomain for those types, which I assumed would be loaded after building.  Luckily my assumptions were correct.


    יום שישי 11 דצמבר 2009 15:19
  • Thanks for the clarification.

    Why am I doing it?  I'm using DTE to scan the current instance of VS from within a Workflow 4.0 custom activity designer to find Pages and UserControls which users will designate as a template to be used within the wma workflow.  I need to spin up these templates in order to take a snapshot of them for use in the designer UI.

    I eventually used the x:Class attribute to search the current AppDomain for those types, which I assumed would be loaded after building.  Luckily my assumptions were correct.



    This is what I'm looking for, Thanks for your sharing! Nice writing.
    יום חמישי 16 ספטמבר 2010 11:10