locked
Loading Xaml + events?? RRS feed

  • Question

  • Hi,

    I am creating a UserControl which writes an object in XAML using XamlWriter after designing it dynamically. That object consists of soem buttons and their event handlers.
    After that I have to load that existing XAML in another WPF application, can those events be fired by just adding a reference of already designed UserControl?


    Thanks
    Sunday, October 28, 2007 9:08 PM

Answers

  • Hi ,

       I think this has been answered by other members here, but I cannot find the original thread.

       XamlWriter cannot serialize the event handlers. XAML serialization is different from BAML compilation, XAML serializer doesn't know if a specified event handler has been attached to a specified event. For instance, you have a piece of code as below:

     

       btn.Click += ButtonEventHandler;

       private void ButtonEventHandler(Object sender, RoutedEventArgs e)

       {

               //...

       }

      

       And when XAML serializer (aka XamlWriter) serializes the XAML which contains a Button called "btn", XAML serializer doesn't know that btn's Click event has been registered by other piece of code, so simply it cannot find the event handler name, and serialize it. This is a limitation imposed by XamlWriter implementation, and is also well documented in MSDN documentation:

        http://msdn2.microsoft.com/enus/library/ms754193.aspx#Event_Handling_is_Not_Preserved

       Also, Mike Hillberg blogs about other interesting aspects of XamlWriter:

            http://blogs.msdn.com/mikehillberg/archive/2006/09/16/XamlWriter.aspx

     

     

       Hope this helps


     

    Wednesday, October 31, 2007 12:33 PM

All replies

  • Hi ,

       I think this has been answered by other members here, but I cannot find the original thread.

       XamlWriter cannot serialize the event handlers. XAML serialization is different from BAML compilation, XAML serializer doesn't know if a specified event handler has been attached to a specified event. For instance, you have a piece of code as below:

     

       btn.Click += ButtonEventHandler;

       private void ButtonEventHandler(Object sender, RoutedEventArgs e)

       {

               //...

       }

      

       And when XAML serializer (aka XamlWriter) serializes the XAML which contains a Button called "btn", XAML serializer doesn't know that btn's Click event has been registered by other piece of code, so simply it cannot find the event handler name, and serialize it. This is a limitation imposed by XamlWriter implementation, and is also well documented in MSDN documentation:

        http://msdn2.microsoft.com/enus/library/ms754193.aspx#Event_Handling_is_Not_Preserved

       Also, Mike Hillberg blogs about other interesting aspects of XamlWriter:

            http://blogs.msdn.com/mikehillberg/archive/2006/09/16/XamlWriter.aspx

     

     

       Hope this helps


     

    Wednesday, October 31, 2007 12:33 PM
  • Thanks a lot for the answer...

    So you mean to say BAML preserves the event handlers?? Can I use that BAML somehow? Can I save that BAML and then convert it back to XAML so that events remain attached to the XAML and can be used later on with assembly?? Is it possible


    Thanks
    Wednesday, October 31, 2007 8:21 PM
  •     Yes, It's possible. You can compile the XAML into BAML, and use Application.LoadComponent to "deserialize" the compiled XAML into object graph, BAML compilation is part of Visual Studio's build system, but as long as you use Visual Studio to author your code, you are fine with it.

     

      Hope this helps

    Thursday, November 1, 2007 4:10 AM
  •  

    Hi,

    Can I have more details on this BAML and how to get .. simple code snippent would be helpful.

     

    I have visited this page but nothing is mentioned here

     

    http://msdn2.microsoft.com/en-us/library/ms754193.aspx

     

    Infact I have tried this also but not served my purpose.

     

    I have loaded content (XAML) in to the canvas. That displays the all the content properly.

    after searching each chield control in the canvas and added event handlers again so that it responds.

     

    But this also failed.

     

    Please give me some hints if you have any..

     

    TIA

    Kishor

    Thursday, November 22, 2007 12:38 PM