Page view counter
Silverlight User Control Inheritance
Last post 10-29-2008 8:39 AM by gabtub. 61 replies.
Sort Posts:
03-24-2008 3:19 AM
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?

caperaven

Loading...
Joined on 06-02-2006
Posts 105
03-24-2008 3:28 AM
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!)

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-24-2008 7:50 AM
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 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
03-24-2008 8:15 AM
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!)

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-24-2008 8:29 AM
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 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
03-24-2008 9:57 AM
Marked as Answer
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!)

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-25-2008 2:56 AM
Re: Re: Silverlight User Control Inheritance

Thanks guys, will test it tonight.

caperaven

Loading...
Joined on 06-02-2006
Posts 105
03-25-2008 1:44 PM
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!

yourbuddypal

Loading...
Joined on 01-14-2008
Posts 82
03-26-2008 9:22 AM
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!)

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-26-2008 9:56 AM
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.)

egoZd

Loading...
Joined on 03-13-2008
china
Posts 110
03-26-2008 11:34 PM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-14-2008 3:28 AM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-14-2008 3:34 AM
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.

caperaven

Loading...
Joined on 06-02-2006
Posts 105
04-14-2008 3:42 AM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-14-2008 3:49 AM
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!!!" ;)

 

caperaven

Loading...
Joined on 06-02-2006
Posts 105
04-14-2008 4:02 AM
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

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-14-2008 4:16 AM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-15-2008 4:40 AM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-15-2008 7:28 AM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-15-2008 7:56 AM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-15-2008 12:04 PM
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

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-15-2008 12:31 PM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-15-2008 12:32 PM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-15-2008 12:48 PM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-15-2008 1:23 PM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-22-2008 11:11 AM
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!

Necroman

Loading...
Joined on 04-17-2008
Posts 7
04-22-2008 11:22 AM
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

sinosoidal

Loading...
Joined on 01-07-2008
Braga, Portugal
Posts 373
04-22-2008 11:22 AM
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!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,335
04-22-2008 11:45 AM
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>

Necroman

Loading...
Joined on 04-17-2008
Posts 7
04-22-2008 11:46 PM
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

Filipus

Loading...