Page view counter
User controls and inheritance
Last post 04-15-2008 4:28 PM by yourbuddypal. 14 replies.
Sort Posts:
03-07-2008 6:12 PM
User controls and inheritance

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 Crying.

yourbuddypal

Loading...
Joined on 01-13-2008
Posts 82
03-08-2008 1:09 AM
Marked as Answer
Re: User controls and inheritance

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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
03-08-2008 3:37 AM
Re: User controls and inheritance

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. Thank you!)

Kind Regards,
Rob Houweling

Visit my blog at http://web-snippets.blogspot.com

robhouweling

Loading...
Joined on 03-01-2008
Ede (the Netherlands)
Posts 397
03-08-2008 4:37 AM
Re: User controls and inheritance
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
03-08-2008 5:45 AM
Re: User controls and inheritance

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

yourbuddypal

Loading...
Joined on 01-13-2008
Posts 82
03-08-2008 6:37 AM
Re: User controls and inheritance

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. Thank you!)

Kind Regards,
Rob Houweling

Visit my blog at http://web-snippets.blogspot.com

robhouweling

Loading...
Joined on 03-01-2008
Ede (the Netherlands)
Posts 397
03-08-2008 9:04 AM
Re: User controls and inheritance

 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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
03-08-2008 12:50 PM
Re: User controls and inheritance

<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. Thank you!)

Kind Regards,
Rob Houweling

Visit my blog at http://web-snippets.blogspot.com

robhouweling

Loading...
Joined on 03-01-2008
Ede (the Netherlands)
Posts 397
03-08-2008 5:34 PM
Re: User controls and inheritance

 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.
 

yourbuddypal

Loading...
Joined on 01-13-2008
Posts 82
03-10-2008 3:53 AM
Re: User controls and inheritance

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. Thank you!)

Kind Regards,
Rob Houweling

Visit my blog at http://web-snippets.blogspot.com

robhouweling

Loading...
Joined on 03-01-2008
Ede (the Netherlands)
Posts 397
03-18-2008 4:42 AM
Re: User controls and inheritance

So if I well understand you do not create a new a new UserControl but a new CS class which inherite from UserControl.

So you don't have XAML ? 

Fabien Warniez
Accenture TechLabs Sophia-Antipolis
Accenture Technology Labs
www.topbutpes.com

Waar

Loading...
Joined on 02-11-2008
Posts 93
03-18-2008 5:20 AM
Re: User controls and inheritance

Waar:

So if I well understand you do not create a new a new UserControl but a new CS class which inherite from UserControl.

So you don't have XAML ? 

 

Yes. because he want to use one function (not ui)  from base class.

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

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
03-18-2008 1:59 PM
Re: User controls and inheritance

 Correct.  My user control class does not have xaml, but the pages that inherit from it are just "normal" SL pages, so they do.  In the backend code for those SL pages, they can use functions defined in my class.

yourbuddypal

Loading...
Joined on 01-13-2008
Posts 82
04-15-2008 2:51 PM
Devil [6]Re: User controls and inheritance

 What about moving those helper methods to a helper class, rather than inheriting them from a base class?  That would solve your problems, and improve your design in one stroke.  

There are some very good design reasons to not extend a class for the sole purpose of gaining access to it's methods (see the Liskov Design Principle)  Inheritance is a very powerful tool, but it carries with it many dangers.  You are experiencing one of them.  Many people people feel that inheritance should be avoided when possible, and there are several Design Patterns that exist for that purpose.

Instead of having your pages inherit from your helper class, you can take the methods to which you need access, and move them to a seperate helper class.  Your pages can then instantiate that new class, and get the methods they need.  For example:


    public class HelperBase : UserControl {
public void doSomethingUseful(){
//do some trick } } public class MyPageClass : HelperBase {
public MyPageClass(){
InitializeComponent();
doSomethingUseful();
}
}
Becomes
    public class Helper  {
public void doSomethingUseful(){
//do some trick } } public class MyPageClass : UserControl {
public MyPageClass(){
InitializeComponent();
Helper helper = new Helper();
helper.doSomethingUseful();
}
}
You still get to keep all the helper code in one place (a good thing!) You also get to avoid fighting with studio over generated code (a good thing.) Most importantly, you get to avoid a potentially nasty downstream maintenance problem (a very good thing.) 
Jody.Breshears

Loading...
Joined on 03-19-2008
Posts 5
04-15-2008 4:28 PM
Re: User controls and inheritance

 Thanks for the post.

 In addition to sharing some common methods, my base class also has a number of member variables that my pages need to have.  Therefore, it does actually make sense for me to be using inheritance.  I will definitely have to consider if i can branch off some of my methods in there to a helper class though, you bring up a great point.
 

yourbuddypal

Loading...
Joined on 01-13-2008
Posts 82
Microsoft Communities