Skip to main content

Microsoft Silverlight

Answered Question Silverlight User Control InheritanceRSS Feed

(0)

caperaven
caperaven

Member

Member

133 points

107 Posts

Silverlight User Control Inheritance

I am writing a custom control.
This control and the others to follow, must inherit from a baseclass that I have defined some generic functions in.

Every time I make changes to the code or xaml, the .g.cs partial class gets regenerated, but it keeps putting UserControl as the base.
This means that it does not compile anymore...

Can I spesify somewhere for the code generator what base class I want it to use for the .g.cs partial class?

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Hello caperaven,

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>

6.[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "YourNamespace")]

In the class library project's AssemblyInfo.cs file, add this:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "YourNamespace")]

Now after you add reference to this assembly in your main project, you can use any classes in this namespace in your XAML files.

 

(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


Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Michael,

What he's asking is if there's a way to do something like this:

public class BaseControl : UserControl
{
}

public class InheritedControl : BaseControl
{
}

The problem is that there's no way to tell the code generator that the InheritedControl's generated partial class should be inherited fron BaseControl and not UserControl. In WPF you can replace the <UserControl> tag in the xaml with something like <my:BaseControl> and refer to the correct namespace and it will work, but this doesn't work in Silverlight 2.

This is a known bug and the Silverlight team is aware of it, hopefully it will be fixed for the next release. I don't know of any good workarounds for it at this point, I've tried a bunch of different things to get around this.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Hello Bill,

Thanks for that.

I have tried the way that I mentioned in my previous thread. I made a few changes since the requirement is the nested controls but it is working fine.  

1. Create new silverlight project called "SL2Controls" 

2. Add a class called BaseControl

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL2Controls {
    public class BaseControl : UserControl {

    }
}
 

3. Add Silverlight User control called "InheritedControl" 

XAML

<BaseControl x:Class="SL2Controls.InheritedControl"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="Red">

    </Grid>
</BaseControl>
 

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL2Controls {
    public partial class InheritedControl : BaseControl {
        public InheritedControl() {
            InitializeComponent();
        }
    }
}
 

4. Add mapping in AssemblyInfo.cs under Properties of SL2Controls

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Markup;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SL2Controls")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SL2Controls")]
[assembly: AssemblyCopyright("Copyright ©  2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "SL2Controls")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("27885afb-0308-473e-9773-73350bc1555f")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
 

5. Add this Inherited control to Page.xaml.

<UserControl x:Class="SL2Controls.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myctl="clr-namespace:SL2Controls"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <myctl:InheritedControl/>
    </Grid>
</UserControl>

then, run the application.  (You don't need to update *.g.cs file.) 

Download Source: http://michaelsync.net/demo/SL2Controls.zip

There is one weakness in this trick. You will lost the designer in InheritedControl.xaml but you won't get any error.

Feel free to let me know if I was missing something or etc.. I'm not sure whether this is a known issue or not. but it works. Feel free to let me know if you have any comment or suggestion. I love to try more.  :)

(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


Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Ah pretty cool, looks like you may have found the workaround I was looking for. The step I was missing was the AssemblyInfo.cs stuff.

Thanks,
Bill 


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP
Answered Question

Re: Silverlight User Control Inheritance

Bill Reiss:
The step I was missing was the AssemblyInfo.cs stuff.
 

Yes. I didn't know about that earlier. but Yi-Lun explained me like that below in this post. Yi-Lun is a great person. :)

Yi-Lun:
 

Hello, there're some issues related to UserControl inheriting that we're investigating... I'm surprised to see this works:

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

Actually it shouldn't work. MyUserControlBase is not in the namespace http://schemas.microsoft.com/client/2007. This xml namespaces maps to a series of clr namespaces such as System.Windows.Controls. But there's no way it can map to your own namespace. Also this behavior is inconsistent with WPF.

This is likely to be a bug. I'll redirect this to our product team. Thanks for letting us know.

You're right. This can be a work round.

In the class library project's AssemblyInfo.cs file, add this:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "YourNamespace")]

Now after you add reference to this assembly in your main project, you can use any classes in this namespace in your XAML files.

But without the namespace mapping, your original code should not work. So this still seems to be an issue...

 



(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


caperaven
caperaven

Member

Member

133 points

107 Posts

Re: Re: Silverlight User Control Inheritance

Thanks guys, will test it tonight.

yourbuddypal
yourbudd...

Member

Member

198 points

82 Posts

Re: Re: Silverlight User Control Inheritance

 Ooo, I had a similar implementation for this working, but did not have the modified AssemblyInfo code.  That gets rid of the error message, thanks!

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Re: Silverlight User Control Inheritance

yourbuddypal:
did not have the modified AssemblyInfo code
 

I already mentioned the reply from Yi-lun in my previous post. This is a bug if it's working fine without adding mapping in assemblyinfo.

(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


egoZd
egoZd

Member

Member

326 points

125 Posts

Silverlight User Control Inheritance

Im very happy to found this post,You know when very time a modify some thing or add a new element to the partcual form ,i need to rechange the inherited class name,It maked me crazy.

Right now in my project i coud only do is very child class write a some method , using reflese to get the method by name. It's also make me crazy.

Thank you very much. Ill try it.

Something remember ,Something forgot!


www.douziwang.cn

(My Silverlight Blog Jet,Silverlight game ect.)

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

egoZd:

Im very happy to found this post,You know when very time a modify some thing or add a new element to the partcual form ,i need to rechange the inherited class name,It maked me crazy.

Right now in my project i coud only do is very child class write a some method , using reflese to get the method by name. It's also make me crazy.

 

Both ways make you crazy? :) haha.

egoZd:
Thank you very much. Ill try it.
 

Your welcome! Good luck. :) 

(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


sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

I'm having problems in making my specialized control to work. It is compling fine but when executed gives me an error which i can't find out from where it comes...

 AG_E_PARSER_BAD_TYPE [Line: 4 Position: 29]

I have a SectionControl class that inherits from UserControl and implements an interface. I substituted a control i already had for this one and it gives error on execution.

How can i find what is wrong?

Any tip?


Thx,

Nuno
 

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

caperaven
caperaven

Member

Member

133 points

107 Posts

Re: Silverlight User Control Inheritance

As far as i remember, this means you may have wrong / unsupported markup in your xaml.

Debugging it will not help as you can't debug xaml markup.

This will be a painfull experiance for you, so be prepaired.

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

hehehe

That was really encouraging! :)

Thx

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

caperaven
caperaven

Member

Member

133 points

107 Posts

Re: Silverlight User Control Inheritance

Sorry dude, i had that problem once and it was NOT fun.

What I did was comment out blocks of xaml until it got working again.
Then started plugin stuff back in bit by bit to see if it will work again.

Some times you changed the name of a resource or something that is now can't be found and causes this type of error.

Encouragement: "You can do it!!!" ;)

 

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

I think i was misunderstood. You are completly right and i have already passed for some situations in silverlight that are desperating.

The only way is really what you have just said. Comment, and lost a lot of time.

Sorry if i made you feel unconfortable.

Thx! 

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

When i did this step for the specialization of the user control, i had to insert the assembly line:

 [assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "PRW")]

 But now, in Blend, it complains about

<Style TargetType="MenuButton" x:Name="SectionButtonStyle" >
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="MenuButton">                 <-----
      <Grid x:Name="RootElement" Background="{TemplateBinding Background}">
       <Grid.Resources>

 Saying:

Invalid attribute value MenuButton for property TargetType.

Which is a custom control i have made.

Maybe this is the problem that is making that exception to be raised, but i cant find why is this making a problem.

Any tips?

Thx,

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

sinosoidal:
TargetType="MenuButton"
 

I think it will happen like that. If you look at the style for Datagrid, it used "local:DataGrid"  not just "Datagrid". So, I think you may need to add some prefix in your style 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


sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

Even if i add the prefix i have errors.

Another thing. I was tryning to do what you suggest in the the top of the thread in order to have the specialized user control and it doesnt work. When running crashes! :S

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Hi,

sinosoidal:
Even if i add the prefix i have errors.
 

If you can make some sample, I would happy to take a look. you can reach me with this address mchlsync AT gmail DOT com. 

sinosoidal:
Another thing. I was tryning to do what you suggest in the the top of the thread in order to have the specialized user control and it doesnt work. When running crashes!
 

Which one? the second one? I think you are doing something totally wrong. The solution that I suggested is working fine for many people. Just create "new" project and follow the steps that I mentioned. I will work. 

(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


sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

I made it! Mega refactoring using the specializing approach. It works perfectly. I don't know what was wrong on the first attempts.

I only have one problem, is with the custom button i have created. Addind the namespace to the default namespace makes the MenuButton an invalid type in styles, even with the prefix. However, it compiles and runnes. It only doesnt work on Blend. Its not a big thing, but it messes my layout a little bit in Blend.

Do you know how to workaround this problem?

Thx,

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi Michael,

I have a more serious problem in Blend. When i open a SectionControl (my inherited class), it says i cant have content, so i cant use Blend at all. :S

How can i work around this?

Thx,

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

mchlSync
mchlSync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

AFAIK, there is no wordaround for that.If we use User control inheritance in Silverlight, we will lost visual designer in Blend and VS.

(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


mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

sinosoidal:
I have a more serious problem in Blend. When i open a SectionControl (my inherited class), it says i cant have content, so i cant use Blend at all.
 

can't use Blend at all? Why? you can still using it but yes, you will get nothing in designer.. Yes. it's bad. ..  What I used to do is that i used Usercontrol (normal one)  if I want to do some designing stuffs and animations.. then, change it back to inherited once after that.

(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


sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Is this a standard behaviour? Is that offical

What does Blend expect from the inherited class that UserControl has and the specialized not?

Can we manually insert something in the class that will make it work?

Oh god...

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

Necroman
Necroman

Member

Member

20 points

7 Posts

Re: Silverlight User Control Inheritance

I've got another problem, very similar to the previous one.
I've got my own User Control with some XAML definition code and I want to inherit from this class:

<UserControl x:Class="Test1.ControlX"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" ShowGridLines="True">
        <Ellipse Width="50" Height="30" Grid.Column="1" Grid.Row="1" />
    </Grid>
</UserControl>

public partial class ControlX : UserControl
{
    public ControlX()
    {
        InitializeComponent();
    }
}

and then
public class ControlTwo : ControlX
{    
    public ControlTwo()
        : base()
    {
    }
}

the program compiles, but when I try to instantiate ControlTwo class, it fails with error:

AG_E_PARSER_BAD_TYPE, on the line with InitializeComponent(); in ControlX.

So, where's the problem? Am I using bad syntax, or am I missing some workaround stuff? Thanks for help!

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

Usually i got that error when there is no definition in code for a event handler that is declared in xaml or when there is not such property in xaml.

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

 Can you show the XAML code for ControlTwo?

(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


Necroman
Necroman

Member

Member

20 points

7 Posts

Re: Silverlight User Control Inheritance

ControlTwo does not have any XAML code, it just derives from ControlX. I guess, that it's not agains rules, but the XAML parser is thinking otherwise.
Small update: Page.xaml contains:

<UserControl x:Class="Test1.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:Test1"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" >

        <i:ControlTwo Width="100" Height="100"/>

    </Grid>
</UserControl>

Filipus
Filipus

Member

Member

20 points

13 Posts

Re: Silverlight User Control Inheritance

 Hi all,

 I am having the exactly same error. When I put on the page just the base class, it works fine. But if I put there the inherited one it throws unhandled exception (AG_E_PARSER_BAD_TYPE) in InitializeComponent() in base class! 

the inherited class looks like this:

XAML

 <BaseListItemControl x:Class="ItemsControls.TestItemListControl"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</BaseListItemControl>

CS:

namespace ItemsControls
{
    public partial class TestItemListControl : BaseListItemControl
    {
        public TestItemListControl()
        {
            InitializeComponent();
        }
    }
}

 

 

-Filip

marlun017
marlun017

Member

Member

2 points

1 Posts

Re: Silverlight User Control Inheritance

I have the exact same problem! Unfortunately i don't have a solution for it but i think i know the reason for the error message.

The g.cs file is trying to load the base class xaml into the inherited class, it doesn't try to load the xaml into the base class and then instantiate the inherited class which would work.

Maybe not inheriting from a base class and instead load the base class component from the previously inherited class would work, this it the approach i'm going to test.

 Regards,

Marcus

JeffWeber
JeffWeber

Member

Member

139 points

92 Posts

Re: Silverlight User Control Inheritance

Anybody know what the status of this issue is for Beta 2? 

 I was hoping it would be fixed, but I'm still having issues.  The workaround used in Beta1 isn't even working at the moment.

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

This works for me, what are your issues?


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

caperaven
caperaven

Member

Member

133 points

107 Posts

Re: Silverlight User Control Inheritance

Sorry dude, problem still exists.
Seems we will have to wait a little longer?

JeffWeber
JeffWeber

Member

Member

139 points

92 Posts

Re: Silverlight User Control Inheritance

Ok, in an attemt to get this working, I created a small test solution and tried creating a simple "BaseControl" that inherits from UserControl

When I create a control that inherits from "BaseControl" and update the XAML and AssemblyInfo.cs as discussed in this post and others, the app compiles, but throws a runtime error when trying to run "InitializeComponent" on the inherited control.

 Here is the error I'm seeing: http://www.farseergames.com/HostedImages/inheritbug.png

Here is the test project I'm using: http://www.farseergames.com/HostedFiles/ControlInheritanceTest.zip

 Any help on this is appreciated.

 Jeff Weber

epasi
epasi

Member

Member

18 points

5 Posts

Re: Re: Silverlight User Control Inheritance

I have exactly the same problem, please someone help me , my app is all based on this :-(

shahed
shahed

Member

Member

51 points

21 Posts

Re: Silverlight User Control Inheritance

Hi,

I also had a similar problem.
The base classes (including user controls) are in one class library and other classes (from a different class library) needs to use those classes (customized user control).

Here is how I solved this:
http://www.shahed.net/post/Implementing-your-own-base-class-for-user-controls-in-Silverlight-2.aspx
 

Thanks,
Shahed

indigoblue
indigoblue

Member

Member

52 points

33 Posts

Re: Silverlight User Control Inheritance

But the difference with your example is that the root custom control doesn't have a xaml file associated with it. If you subclass a control that has an a xaml file, it doesn't work as described above.

My project also depends on subclassing with a Xaml file...it works in WPF and worked in B1 (via workarounds)...does anyone have either any idea of how to do this in B2, or can someone from MS tell us what we're supposed to do to get this to work (or when it will...)?

 Thanks in advance...

 pq  

JeffWeber
JeffWeber

Member

Member

139 points

92 Posts

Re: Silverlight User Control Inheritance

Thanks Shahed, that's exactly what I was looking for.

 I'm not using xaml with my base class so that's not an issue for me.

 -Jeff

Bill Reiss
Bill Reiss

Contributor

Contributor

4818 points

913 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

Jeff,

I looked at your project, and fixed your error. You need to namespace the BaseControl tag in the InheritedControl.xaml. It needs this so that it knows that BaseControl is coming from your assembly. It will look like this:

<my:BaseControl x:Class="ControlInheritanceTest.TestUserControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300" xmlns:my="clr-namespace:ControlInheritanceTest">

<Grid x:Name="LayoutRoot" Background="Red">

</Grid>

</my:BaseControl>


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

JeffWeber
JeffWeber

Member

Member

139 points

92 Posts

Re: Silverlight User Control Inheritance

Yep, that's what I was missing. Thanks, Bill.

JeffWeber
JeffWeber

Member

Member

139 points

92 Posts

Re: Silverlight User Control Inheritance

Well, I've got this working in Visual Studio, unfortunately, Blend still doesn't know how to render it.  Blend does not recognize the namespace scoped tag for the base class and requires reverting back to "UserConrol" for the opening and closing tags.

 That is very unfortunate and I hope the team will address this before the final version. 

 -Jeff

Tim Favour
Tim Favour

Member

Member

93 points

69 Posts

Re: Silverlight User Control Inheritance

Hi,

I'm trying to inherit a control (TestControl) from a base control that inherits UserControl with no success, the solution does not compile with the error:

Partial declarations of Viewer.Client.TestControl' must not specify different base classes

 Does control inheritance work in beta 2 ?

Tim

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

Hi,

I also would like to know if it is possible to see in blend an inherited class in beta 2  as that wasn't possible on the beta 1.

Thx,

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

shahed
shahed

Member

Member

51 points

21 Posts

Re: Silverlight User Control Inheritance

Hi Tim,

It works in beta 2.

Can you please check the xaml. Generally you get this error if xaml and the code refers to different base class. For ex: in my case, the base class is UserControlBase.

So, the xaml is:

<bl:UserControlBase x:Class="CustomControls.TestControl"
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:bl="clr-namespace:BaseLibrary;assembly=BaseLibrary"
Width="150" Height="50">
...
...

and,

public partial class TestControl : UserControlBase
{
...
...
}

- Shahed

Thanks,
Shahed

Tim Favour
Tim Favour

Member

Member

93 points

69 Posts

Re: Silverlight User Control Inheritance

Thanks Shahed, your post solved a lot of problems !

Tim

sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

 

Hi,

Sorry to ask again, but can someone tell me if in beta 2 we can use blend to edit inherited classes from UserControl?

Thx,

Nuno 

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Silverlight User Control Inheritance

sinosoidal:
can someone tell me if in beta 2 we can use blend to edit inherited classes from UserControl?
 

It should be editable. why? Are you having some problems?  

(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


sinosoidal
sinosoidal

Member

Member

664 points

373 Posts

Re: Silverlight User Control Inheritance

In beta 1 it was not possible.

I think you were there person who confirmed me that in this same thread.

Can you assure me that?

Thx,

Nuno

--

Imagining (and touching) the future
http://www.nunosantos.net/
http://www.myspace.com/sinosoidal

dotnethero
dotnethero

Member

Member

61 points

31 Posts

Re: Silverlight User Control Inheritance

I have your solution! I just worked all this out.

First, create your base class and have it inherit UserControl.

Add your new UserControl, and have it inherit your base class.

Right click the xaml file, and go to Properties. Change the Build Action to resource.  Delete the .g.cs file. It will no longer be generated.

Modify the constructor for your control, and manually call LoadComponent().  Or pass the xaml path to the base class constructor, which is how I do it:
Application.LoadComponent(this, new Uri("/YourNameSpace;component/" + xamlPath, UriKind.Relative));

The reason for that AG Parser Error or whatever, is because the x:Class attribute in your xaml, must match the class you are loading.  Setting it up the way I explained will resolve this.

InquisitorJax
Inquisit...

Member

Member

80 points

60 Posts

Re: Silverlight User Control Inheritance

 dotnethero...

For me, it was easier just to cut and paste the code from the .g.cs file (which contains the LoadComponent() ) - since it also as all the UI accessor variables and initialization.

ie: Leave the InitializeComponent() method call in the derived class constructor, and paste the code from the .g.cs file that has this method and UI variable definitions.
I've put this code into a "temp" region, so it can easily be removed when M$ get round to fixing the bug.

 

I'm still getting the AG_PARSER  error though. I've probably missed a step somwhere :-|

Will this still work if my base class definition is in the same assembly, or does it have to be in another project?

InquisitorJax
Inquisit...

Member

Member

80 points

60 Posts

Re: Silverlight User Control Inheritance

okay - it seems as if I've gotten rid of the AG_E_PARSER error - turns out that it matters where you define the baseControl xmlns property.

I had it defined after the width and height property settings.

Moved it next to all the other xmlns properties, and no more error.

For a split second I saw my Designer UI (woohoo!), and then VS crashed (Boooooo!)

Now VS crashes every time I try and open the xaml file Sad

anyone know where I can look (VS debug files or something) for details around why it's crashing?

EDIT: SideIssue:

It seems VS is moaning about my BaseControl not having a property 'Content' (shows as a compile warning)
but I see that shahed's SampleApp has that warning too, so it's prob a red herring.
Blend says "Cannot add content to an object of type MyControlBase" when trying to open TestControl.xaml (which derives from MyControlBase)
Funny thing - when opening Page.xaml which contains TestControl, the TestControl renders fine. ?!

 


shahed
shahed

Member

Member

51 points

21 Posts

Re: Silverlight User Control Inheritance

Hi InquisitorJax,

The Visual Studio designer works fine with custom user control. I have updated my post. You need to add the following reference in AssemblyInfo.cs file:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "<your assembly name>")]

you can see the full process here.

Thanks,
Shahed

InquisitorJax
Inquisit...

Member

Member

80 points

60 Posts

Re: Silverlight User Control Inheritance

 Shahed,

 The VS designer works fine with or without the assemblyinfo entry.

I was referring to the Designer in Blend that won't render the TestControl from your example (but will render Page.xaml if

you add TestControl to the xaml)

Jax

syntax42
syntax42

Member

Member

6 points

9 Posts

Re: Re: Silverlight User Control Inheritance

Just to clearify things. Was this issue ever resolved with at workaround? I still get the AG_E_PARSER_BAD_TYPE runtime error message. I have tried every suggestion on this thread.

Is it a bug that will be fixed in the final release? (I hope so Smile)

caperaven
caperaven

Member

Member

133 points

107 Posts

Re: Re: Silverlight User Control Inheritance

The parse error basicially means that there is a problem with your xaml vs backend code.

Could be something stupid such as a name space change or class name change.
It could be as complicated as a wrong xaml loading path if you made this a resource xaml file and custom load it.

The problem here is that the tools is falling behind the technology.
What I would stuggest is that you look at your xaml header content and make sure you are referencing the right class with the right namespace on the right assembly and all that.

The look at the loading code and make sure you are indeed loading the right file.

Also look at the xaml in VS2008 and if you see a blue squigal line, something in the xaml is wrong.

Your problem may not even be in the xaml you are looking at, but in a control you may have written in a other assembly that is not matching up.

Good luck with resolving your problem.

syntax42
syntax42

Member

Member

6 points

9 Posts

Re: Re: Re: Silverlight User Control Inheritance

(EDIT: wrong link)

Thanks for the quick reply!

 In the meantime i found out what is causing the error message:

http://silverlight.net/forums/t/14936.aspx (the last posts)

It seems there there IS a bug in Beta 2 of Silverlight 2. It occurs if you have a base class WITH XAML and a standard, code-only inherited class. I hope it will be fixed soon (RC?), as my whole application is build around this concept.

plafond444
plafond444

Member

Member

9 points

22 Posts

Re: Silverlight User Control Inheritance

...to get rid of "The property 'Content' does not exist on the type..." warnings in Visual Studio and fix the Blend's "Cannot add content to an object of type..." issue, just add this property to your base class:

        public new UIElement Content
        {
            get { return base.Content; }
            set { base.Content = value; }
        }

note: unfortunately, your base class cannot be abstract if you want to use the visual designer in Blend

rceles
rceles

Member

Member

23 points

12 Posts

Re: Re: Silverlight User Control Inheritance

the Solution in last Post work correctly, but in Silverlight 2 Beta 2, throw a "Catastrofic Error"; for  do this work i do:

set { base.Content=new ContentControl(); ((ContentContol)base.Content).Content=value:}

 

regards.

Roberto.
 

plafond444
plafond444

Member

Member

9 points

22 Posts

Re: Re: Silverlight User Control Inheritance

rceles:

the Solution in last Post work correctly, but in Silverlight 2 Beta 2, throw a "Catastrofic Error"; for  do this work i do:

It works for me in Silverlight 2 Beta 2... From what class do you derive your base class (mine was from UserControl)?

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Re: Silverlight User Control Inheritance

 If it's nested namespace, please check my reply in this post http://silverlight.net/forums/p/23213/83217.aspx#83217

(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


gabtub
gabtub

Member

Member

2 points

1 Posts

Re: Silverlight User Control Inheritance

 i just wondered if there's a fix now in the new version for these inheritance problems?

Isil
Isil

Member

Member

14 points

31 Posts

Re: Silverlight User Control Inheritance

I'm still having problems with this.

I'm getting this exception: 

System.Windows.Markup.XamlParseException occurred
  Message="Unknown Element: ItemControl. [Line: 7 Position: 375]"
  LineNumber=7
  LinePosition=375
  StackTrace:
       en System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       en Exchange_S2.EmptyItem.InitializeComponent()
       en Exchange_S2.EmptyItem..ctor()
  InnerException: 

 that is related to this xaml:
 
<ItemControl x:Class="Exchange_S2.EmptyItem"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
     MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave" 
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
MinWidth="110" MinHeight="110" Padding="0,0,0,0" Margin="10,10,10,10" 
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
d:DesignHeight="200" d:DesignWidth="200" Width="140" Height="140" 
MaxWidth="300" MaxHeight="300">
 
 Any ideas?? I really need to get this fixed.
 

Roet
Roet

Member

Member

211 points

65 Posts

Re: Re: Silverlight User Control Inheritance

Try adding a reference to the namespace where you have the ItemsControl defined, just like you would with any other custom control you define on a page.

Let's say you defined it in the namespace "Exchange_S2".

<exs2:ItemControl x:Class="Exchange_S2.EmptyItem"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
    xmlns:exs2="clr-namespace:Exchange_S2"
"clr-namespace:TodayIT.Avalanche2.Web.Controls.Basic    mc:Ignorable=
"d"
     MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
MinWidth="110" MinHeight="110" Padding="0,0,0,0" Margin="10,10,10,10"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
d:DesignHeight="200" d:DesignWidth="200" Width="140" Height="140"
MaxWidth="300" MaxHeight="300">

 ..

</exs2:ItemControl>

 If the above doesn't work, try the following, on top of the solution mentioned above: Add a reference in AssemblyInfo.cs [assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "Exchange_S2")]

If this post helped you, please mark it as answer! It helps other people too.

Isil
Isil

Member

Member

14 points

31 Posts

Re: Re: Re: Silverlight User Control Inheritance

 Thank you very very much

That fixed my problems... but I don't know what I did, could someone  explain me??

 

thank you!

talldaniel
talldaniel

Member

Member

590 points

325 Posts

Re: Re: Silverlight User Control Inheritance

If you go through all of these contortions, it will likely be the case that when the next upgrade is released, the work arounds will bust.

wjchristenson2
wjchrist...

Member

Member

104 points

51 Posts

Re: Re: Silverlight User Control Inheritance

I've just blogged on how I got UserControl inheritance to work for me.  Hope this helps:

 http://www.mostlydevelopers.com/blog/post/2009/01/21/Silverlight-User-Control-Inheritance.aspx

Bill - Senior .NET Developer
A Mostly Developers Blogger

Please remember to click “Mark as Answer” on the post that helps you.

zuppaman
zuppaman

Member

Member

28 points

4 Posts

Re: Re: Silverlight User Control Inheritance

Hi Guys,

I also encountered the same problem.
Like wjchristenson2 created a blog post about it.

You can solve this problem with two methods.
Creating a wrapper as discussed above our use the strategy pattern.

Check out the code at : http://www.lab101.be/2008/07/silverlight-usercontrol-inheritance/

www.lab101.be
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities