Skip to main content

Microsoft Silverlight

Answered Question User controls and inheritanceRSS Feed

(0)

yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

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.

mchlSync
mchlSync

Star

Star

14566 points

2,730 Posts

Silverlight MVP
Answered Question

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

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)

Cheers!
Rob Houweling



My blog

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

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! 

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

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)

Cheers!
Rob Houweling



My blog

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

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)

Cheers!
Rob Houweling



My blog

yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

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.
 

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

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)

Cheers!
Rob Houweling



My blog

Waar
Waar

Member

Member

166 points

93 Posts

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

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

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.

Jody.Breshears
Jody.Bre...

Member

Member

8 points

5 Posts

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

yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

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.
 

bgulian
bgulian

Member

Member

14 points

7 Posts

Re: User controls and inheritance

 

Jody.Breshears:

 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.  

 

Maybe.  Depends.  I was interested in this UserControl override for the purpose of forcing implementation of abstract methods I declare in my base class in order to facilitate access from sub-controls to the data model which needs to be instantiated in page.xaml.cs.  The "Helpers" do not really help with that design goal.   However, the designer is the only way to tell if my xaml has successfully compiled so the inheritance scheme does not really work for me.

I sure hope the VS 2008 team is working on a patch for the designer.

 

Bob

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities