Skip to main content

Microsoft Silverlight

Answered Question A question on inheritanceRSS Feed

(0)

markk
markk

Member

Member

0 points

3 Posts

A question on inheritance

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

public class PageContent : UserControl

{......}

All UserControl then inherit from the above i.e.

public partial class Home : PageContent

{....}

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 partial class Home : System.Windows.Controls.UserControl {....}

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 :-(


 

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

Re: A question on inheritance

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



My blog

Psychlist1972
Psychlis...

Contributor

Contributor

6040 points

973 Posts

MicrosoftModerator

Re: A question on inheritance

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

Silverlight.net Moderator
MVP: Silverlight, Silverlight Insider
Author: Silverlight in Action, 2nd Edition
POKE 53280,0 - My Blog

Opinions are my own. Oh, and I don't work for Microsoft.

kgalenko
kgalenko

Member

Member

50 points

18 Posts

Re: A question on inheritance

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 

 

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

Re: A question on inheritance

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

Kind regards,

Rob Houweling.

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

Cheers!
Rob Houweling



My blog

RodKestler
RodKestler

Member

Member

30 points

10 Posts

Re: A question on inheritance

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.

-Rod Kestler
CTO, Hatch Innovations, LLC

RodKestler
RodKestler

Member

Member

30 points

10 Posts

Re: A question on inheritance

So, it's a "limitation" - any idea when this "limitation" might be "optimized"?

-Rod Kestler
CTO, Hatch Innovations, LLC

RodKestler
RodKestler

Member

Member

30 points

10 Posts

Re: A question on inheritance

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:

  1. Make sure you have added a System.Windows.Application derived Application object to your Silverlight application.
  2. Then set the application as the startup object from the Silverlight Application properties pages (Silverlight tab, Startup Object dropdown)
  3. The default application implementation supplied by Visual Studio will hook the Application event called "Startup" and provide you with an empty event handling method. The method will be named "Application_Startup".
  4. In the code for this method, simply instantiate your strongly typed container class/control, assigning it to the Application's RootVisual property
  5. Example:

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.

-Rod Kestler
CTO, Hatch Innovations, LLC

parimaln
parimaln

Member

Member

120 points

100 Posts

Re: A question on inheritance

Hi,

The answer in thi ssolution helped me.

Hope this helps you all.


http://silverlight.net/forums/t/10970.aspx

 regards,

Parimaln

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

Re: A question on inheritance

This...um..."limitation" is on the list of stuff to fix.

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

Re: A question on inheritance

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.  

 

(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: A question on 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".

So it's not a workable solution if my designers can't change the XAML anymore.

(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

85 Posts

Re: A question on inheritance

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. 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities