Skip to main content
Home Forums Silverlight Programming Programming with .NET - General A question on inheritance
12 replies. Latest Post by yourbuddypal on March 10, 2008.
(0)
markk
Member
0 points
3 Posts
03-06-2008 9:27 AM |
Hi everyone..hoping someone may have an answer to this.
I have a UserControl that needs to inherit some base functionality. This base functionality has been placed into a seperate class i.e
{......}
All UserControl then inherit from the above i.e.
{....}
However when compiling you receive the error : Error 2 Partial declarations of a.POC.Silverlight.Content.Home' must not specify different base classes d:\a\v2\a.poc.silverlight\a.poc.silverlight\obj\debug\content\home.g.cs 35 26 a.POC.Silverlight.
Which is understandable because the generated file Home.g.cs contains the following constructor definition:
public
If I change the inherited class System.Windows.Controls.UserControl to PageContent, all is well. However if I then edit the Home.xaml file, the autogenerated file gets regenerated with the System.Windows.Controls.UserControls...
Is there anyway around this. It all seemed fine in SL1.1 however in 2.0 not so :-(
robhouwe...
Contributor
3158 points
540 Posts
03-06-2008 10:23 AM |
Hi,
I'm having the same problem here. Removing the inheritance from the generated class (as it is in a webcontrol in asp.net) is also a workaround with the same result (saving XAML returns the inheretence).
Looks like a bug to me. It should be possible to use inheritance from your codebehind.
Please let me know if there's a solution.
Kind regards,
Rob Houweling
(If this has answered your question, please click on mark as answer on this post)Cheers!Rob Houweling
Psychlis...
6040 points
973 Posts
03-06-2008 10:40 AM |
There's a limitation in Silverlight 2 Beta 1 that does not allow you to markup anything that derives from a custom class (even if that class derives from UserControl).
It's something you'll need to work around for now.
Pete
kgalenko
50 points
18 Posts
03-06-2008 11:02 AM |
To make it happen you have to update 2 files, c# and xaml all together.
Your xaml file references UserControl class and you need to replace it with your custom class PageContent like this:
<src:PageContent x.Class="Home"
...
xmlns:scr="clr-namespace:YourNameSpaceHere">
>
....
</src:PageContent>
that will tell compiler to use your custom class as base class and look for it in your assembly. But I found another problem. While I can compile and see control in VS 2008, Blend can't render it, which I can understand, but when you run the app every time silverlight tries to render this control it produces a parser error: AG_E_UNKNOWN_ERROR.
It looks like it works ok for desktop apps but not for silverlight.
Konstantin
03-06-2008 1:43 PM |
Hi Pete,
Thanks for the quick reply.
Do you know is this limitation is going to be changed in one of the next versions or is this going to remain like this from now on?
And, do you know what the reason for this limitation is? It wasn't like this in SL1.1 alpha and it is different in ASP.NET (perhaps also in WPF?)
Rob Houweling.
RodKestler
30 points
10 Posts
03-06-2008 2:50 PM |
Indeed, I can corroborate the error. Basically, you can't inherit beyond the base implementation of the outer container type. So if you have a a container the inherits directly from UserControl, the parser can handle it, but if you derive from another base class, with inherited functionality, it will throw the AG_E_UNKNOWN_ERROR. This was not the case in Alpha 1.1, so logic dictates this is not only a bug, but a serious show stopper bug.
Actually, this build is terrible. TextBox and WatermarkTextBox do not even scroll correctly when text longer than the width of the text box is entered. Luckily I have TextBoxes that do work - very sad Microsoft. I guess no one bothered to unit test. Very amateur. And now we have an inheritance related parser error which prevents developers from using any polymorphic outer container types, which I'm going to wager is a COMMON situation.
If anyone figures out a workaround for the parser-inheritance issue, please email me at rod@hatchinnovations.com. I'll do the same. I'm looking at possible adapter patterns that can work around this embarassing kludge. Shame on you Microsoft. I am embarassed for you.
03-06-2008 3:27 PM |
So, it's a "limitation" - any idea when this "limitation" might be "optimized"?
03-06-2008 10:01 PM |
I had previously posted that this was a bug I had confirmed myself, which remains true. The workaround however is easy and has desirable side effects in terms of design. The correct means / workaround of instantiating the outer most container for a full-page experience - for instance in the case where you have a page-like type which has some extensive base functionality, such as layout handling - you will want to insure that you are using the new Silverlight Xap architecture. So, you would:
public DemoApp(){ this.Startup += this.Application_Startup; this.Exit += this.Application_Exit; this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();}
private void Application_Startup(object sender, StartupEventArgs e){ this.RootVisual = new DemoPage(); //This can be any type in the application assembly/project or any other referenced Silverlight assembly}
This is a reasonable and desirable workaround, since you get the benefits of the Xaml Application, which provides the custom/stock splash screen feature. I am sure this parser issue was a side-effect of a "by design" decision, and/or the direct Xaml load case was not caught during unit testing. In any case, the alternative is more desirable in overall behavior. I hope this helps anyone who may be puzzled by the AG_E parser error from the previous loading technique typically used in Alpha 1.1.
parimaln
120 points
100 Posts
03-09-2008 4:44 AM |
The answer in thi ssolution helped me.
Hope this helps you all.
http://silverlight.net/forums/t/10970.aspx
regards,
Parimaln
Dave Relyea
Participant
1084 points
249 Posts
03-09-2008 11:19 AM |
This...um..."limitation" is on the list of stuff to fix.
mchlsync
Star
14606 points
2,730 Posts
03-09-2008 11:31 AM |
Dave Relyea:This...um..."limitation" is on the list of stuff to fix.
I dont think this is a limitation. I have tested and posted the solution in this post
It works for me and a few people said that my solution works for them too.
03-10-2008 3:52 AM |
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".
So it's not a workable solution if my designers can't change the XAML anymore.
yourbudd...
198 points
85 Posts
03-10-2008 4:30 AM |
robhouweling: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". So it's not a workable solution if my designers can't change the XAML anymore.
To design the code, you need to change the outer element back to the UserControl only for editing. When done editing, it should be changed back to whatever your custom class name is. The only downside is that you cannot see the designer in VS.