Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

User controls and inheritance RSS

15 replies

Last post Apr 14, 2009 03:44 PM by bgulian

(0)
  • yourbuddypal

    yourbuddypal

    Member

    198 Points

    85 Posts

    User controls and inheritance

    Mar 07, 2008 10:12 PM | LINK

    For my 1.1 app, I had written a base class to use for several of my pages (sharing some common methods, members, etc).  In the alpha release, the user controls were full classes (not partial), but in 2 it looks like theyre partial classes, where they are partial defined in the .g.cs file that is generated on build.  If I attempt to have my page inherit from my custom base class (which inherits UserControl), it throws an error because in the generated file it is inheriting from UserControl.  Is there any way to... make this work?  Removing the "partial" keyword doesnt work either [:'(].

  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: User controls and inheritance

    Mar 08, 2008 05:09 AM | LINK

    Hello,

     

    Please try the following steps.

    1. Create SL project.
    2. MyUserControlBase.cs
    3. Inherits from UserControl

      namespace SL2Test {
          public class MyUserControlBase : UserControl {
              public string DoSomething() {
                  return "Ahh! bad attemps!";
              }
          }
      }


    4. In Page.xaml.cs

      namespace SL2Test {
          public partial class Page : MyUserControlBase {
              public Page() {
                  InitializeComponent();

              }

          }
      }


    5. In Page.xaml,

      <MyUserControlBase x:Class="SL2Test.Page"
          xmlns="http://schemas.microsoft.com/client/2007"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Width="400" Height="300">
          <Canvas Width="400" Height="300" Background="Red">
          </Canvas>
      </MyUserControlBase>

     

    (If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

    Regards,
    Michael Sync
    Silverlight MVP

    Blog : http://michaelsync.net
  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: User controls and inheritance

    Mar 08, 2008 07:37 AM | LINK

    Hi Michael,

    This seems to build fine, but when I run it, I get an InitializeError #2103 (Invalid or malformed application: Check manifest).

    I found a way to get it working (See this post: http://silverlight.net/forums/t/10763.aspx), but the downside with that is you have to manually change the .g.cs class everytime you save the xaml which inherits a different class than the standard UserControl class.

     

     Kind regards,

     Rob Houweling

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: User controls and inheritance

    Mar 08, 2008 08:37 AM | LINK

    Hello Rob,

    robhouweling

    This seems to build fine, but when I run it, I get an InitializeError #2103 (Invalid or malformed application: Check manifest).
     

    It's strange. I have tested in my machine and It was working fine. Are you testing my code with new project?  

    (If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

    Regards,
    Michael Sync
    Silverlight MVP

    Blog : http://michaelsync.net
  • yourbuddypal

    yourbuddypal

    Member

    198 Points

    85 Posts

    Re: User controls and inheritance

    Mar 08, 2008 09:45 AM | LINK

    That looks like its working.  I had tried something similar, but made some stupid mistakes in my previous attempt - Thank you for the post! 

  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: User controls and inheritance

    Mar 08, 2008 10:37 AM | LINK

    Hi Michael,

    Just tried it again in a new project (first attempt was in an existing one) and it's working fine now.

    I think I messed something up with namespaces or something like that. Sorry about that and thanks for the help, I'm very happy that it's possible to do this after all (have to set something straight in my blogpost now ;) )

    Kind regards,

    Rob Houweling

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: User controls and inheritance

    Mar 08, 2008 01:04 PM | LINK

     Hi Rob,

    I glad to hear that.

    robhouweling

    (have to set something straight in my blogpost now ;) )
     

    I think you probably need to update your post again. :) I visited your blog and your blog is nice..  full of silverlight articles.. great.. keep on blogging. :) btw, you like blogspot? for me, I like wordpess..

    (If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

    Regards,
    Michael Sync
    Silverlight MVP

    Blog : http://michaelsync.net
  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: User controls and inheritance

    Mar 08, 2008 04:50 PM | LINK

    <offtopic>

    Thanks for the compliments :).

    I'll try to keep it up if I can find the time (which sometimes is difficult with a fulltime job and a family with 2 small kids :) ).

    I just picked the first blogsite that came up and I wanted to say that blogspot didn't let me down so far, but it seems to do so right now... :( I wanted to update my post and when I tried to open the site I got an error.... Maybe I'll have a look at Wordpress then...


    Google    
    Error
     

    Server Error

    The server encountered a temporary error and could not complete your request.

    Please try again in 30 seconds.

     

    </offtopic>

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog
  • yourbuddypal

    yourbuddypal

    Member

    198 Points

    85 Posts

    Re: User controls and inheritance

    Mar 08, 2008 09:34 PM | LINK

     I have been playing with this a bit today.  It works, but has some issues.  First of all, when I change the root element to my custom control (i.e. "MyUserControl"), the designer stops working. The project builds, but if i build it with the xaml page open, it gives an error:

    "Error    4    The type 'MyUserControl' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built." 

     

    When attempting to design the page in Blend, the workaround that I have found is to change the root element back to UserControl temporarily, make your design changes, then change it back to the custom one.  A bit of a hassle, but it works.
     

  • robhouweling

    robhouweling

    Contributor

    3308 Points

    565 Posts

    Re: User controls and inheritance

    Mar 10, 2008 07:53 AM | LINK

    Hi,

    I've been experimenting some more with this. The solution Michael offers seems to be working when only using VS2008. When you open the page.xaml in Blend however, Blend returns the error "Invalid XAML".

    See this post : http://silverlight.net/forums/p/10763/35384.aspx#35384

     

     

    (If this has answered your question, please click on mark as answer on this post)



    Cheers!

    Rob Houweling





    My blog