Page view counter
A question on inheritance
Last post 03-10-2008 4:30 AM by yourbuddypal. 12 replies.
Sort Posts:
03-06-2008 9:27 AM
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 :-(


 

markk

Loading...
Joined on 03-06-2008
Posts 3
03-06-2008 10:23 AM
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. 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-06-2008 10:40 AM
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

If your question was answered, please mark the response as the answer.

Silverlight.net Moderator
MVP: Visual Developer - Client Application Development
POKE 53280,0 - My Blog

Psychlist1972

Loading...
Joined on 10-12-2004
Maryland, USA
Posts 833
03-06-2008 11:02 AM
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 

 

kgalenko

Loading...
Joined on 03-04-2008
Posts 18
03-06-2008 1:43 PM
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. 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-06-2008 2:50 PM
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

Loading...
Joined on 10-24-2007
Posts 10
03-06-2008 3:27 PM
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

Loading...
Joined on 10-24-2007
Posts 10
03-06-2008 10:01 PM
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

RodKestler

Loading...
Joined on 10-24-2007
Posts 10
03-09-2008 4:44 AM
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

parimaln

Loading...
Joined on 01-24-2008
Posts 100
03-09-2008 11:19 AM
Re: A question on inheritance

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

Dave Relyea [MSFT]
http://blogs.msdn.com/devdave

Dave Relyea

Loading...
Joined on 05-09-2007
Posts 245
03-09-2008 11:31 AM
Marked as Answer
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!)

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-10-2008 3:52 AM
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. 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-10-2008 4:30 AM
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. 

yourbuddypal

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