Skip to main content
Home Forums Silverlight Programming Programming with .NET - General User controls and inheritance
15 replies. Latest Post by bgulian on April 14, 2009.
(0)
yourbudd...
Member
198 points
82 Posts
03-07-2008 6:12 PM |
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
Star
14566 points
2,730 Posts
03-08-2008 1:09 AM |
Hello,
Please try the following steps.
robhouwe...
Contributor
3158 points
540 Posts
03-08-2008 3:37 AM |
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
mchlsync
03-08-2008 4:37 AM |
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?
03-08-2008 5:45 AM |
That looks like its working. I had tried something similar, but made some stupid mistakes in my previous attempt - Thank you for the post!
03-08-2008 6:37 AM |
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 ;) )
03-08-2008 9:04 AM |
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..
03-08-2008 12:50 PM |
<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...
Server ErrorThe server encountered a temporary error and could not complete your request. Please try again in 30 seconds. </offtopic>
Please try again in 30 seconds.
</offtopic>
03-08-2008 5:34 PM |
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.
03-10-2008 3:53 AM |
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
Waar
166 points
93 Posts
03-18-2008 4:42 AM |
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 ?
03-18-2008 5:20 AM |
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.
03-18-2008 1:59 PM |
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.Bre...
8 points
5 Posts
04-15-2008 2:51 PM |
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(); } }
public class Helper { public void doSomethingUseful(){ //do some trick } } public class MyPageClass : UserControl { public MyPageClass(){ InitializeComponent(); Helper helper = new Helper(); helper.doSomethingUseful(); } }
04-15-2008 4:28 PM |
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
14 points
7 Posts
04-14-2009 11:44 AM |
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