none
Silverlight User Control Inheritance

    Question

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

    Monday, March 24, 2008 3:19 AM

Answers

  • 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. :)

     

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

     



    Monday, March 24, 2008 9:57 AM

All replies

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

     

    Monday, March 24, 2008 3:28 AM
  • 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.

    Monday, March 24, 2008 7:50 AM
  • 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.  :)

    Monday, March 24, 2008 8:15 AM
  • 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 

    Monday, March 24, 2008 8:29 AM
  • 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. :)

     

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

     



    Monday, March 24, 2008 9:57 AM
  • Thanks guys, will test it tonight.

    Tuesday, March 25, 2008 2:56 AM
  •  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!

    Tuesday, March 25, 2008 1:44 PM
  • 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.

    Wednesday, March 26, 2008 9:22 AM
  • 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.

    Wednesday, March 26, 2008 9:56 AM
  • 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.

    Thank you very much. Ill try it.
     

    Your welcome! Good luck. :) 

    Wednesday, March 26, 2008 11:34 PM
  • 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
     

    Monday, April 14, 2008 3:28 AM
  • 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.

    Monday, April 14, 2008 3:34 AM
  • hehehe

    That was really encouraging! :)

    Thx

    Nuno

    Monday, April 14, 2008 3:42 AM
  • 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!!!" ;)

     

    Monday, April 14, 2008 3:49 AM
  • 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

    Monday, April 14, 2008 4:02 AM
  • 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

    Monday, April 14, 2008 4:16 AM
  • 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..

    Tuesday, April 15, 2008 4:40 AM
  • 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

    Tuesday, April 15, 2008 7:28 AM
  • Hi,

    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. 

    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. 

    Tuesday, April 15, 2008 7:56 AM
  • 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

    Tuesday, April 15, 2008 12:04 PM
  • 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

    Tuesday, April 15, 2008 12:31 PM
  • AFAIK, there is no wordaround for that.If we use User control inheritance in Silverlight, we will lost visual designer in Blend and VS.

    Tuesday, April 15, 2008 12:32 PM
  • 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.

    Tuesday, April 15, 2008 12:48 PM
  • 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

    Tuesday, April 15, 2008 1:23 PM
  • 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!

    Tuesday, April 22, 2008 11:11 AM
  • 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

    Tuesday, April 22, 2008 11:22 AM
  •  Can you show the XAML code for ControlTwo?

    Tuesday, April 22, 2008 11:22 AM
  • 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>

    Tuesday, April 22, 2008 11:45 AM
  •  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

    Tuesday, April 22, 2008 11:46 PM
  • 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

    Sunday, April 27, 2008 4:01 PM
  • 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.

    Sunday, June 08, 2008 7:24 AM
  • This works for me, what are your issues?

    Sunday, June 08, 2008 7:57 AM
  • Sorry dude, problem still exists.
    Seems we will have to wait a little longer?

    Sunday, June 08, 2008 11:12 AM
  • 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

    Sunday, June 08, 2008 4:17 PM
  • I have exactly the same problem, please someone help me , my app is all based on this :-(

    Sunday, June 08, 2008 5:28 PM
  • 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
     

    Sunday, June 08, 2008 5:33 PM
  • 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  

    Sunday, June 08, 2008 6:29 PM
  • 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

    Sunday, June 08, 2008 8:15 PM
  • 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>

    Sunday, June 08, 2008 8:20 PM
  • Yep, that's what I was missing. Thanks, Bill.

    Sunday, June 08, 2008 8:57 PM
  • 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

    Monday, June 09, 2008 5:32 AM
  • 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

    Thursday, June 12, 2008 5:46 AM
  • 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

    Thursday, June 12, 2008 7:27 AM
  • 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

    Thursday, June 12, 2008 8:25 AM
  • Thanks Shahed, your post solved a lot of problems !

    Tim

    Thursday, June 12, 2008 10:54 AM
  •  

    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 

    Friday, June 13, 2008 6:32 PM
  • 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?  

    Sunday, June 15, 2008 11:01 PM
  • 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

    Tuesday, June 17, 2008 10:10 AM
  • 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.

    Tuesday, June 17, 2008 2:13 PM
  •  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?

    Wednesday, June 18, 2008 5:11 AM
  • 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. ?!

     


    Wednesday, June 18, 2008 9:37 AM
  • 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.

    Wednesday, June 18, 2008 3:57 PM
  •  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

    Thursday, June 19, 2008 2:01 AM
  • 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)

    Wednesday, July 02, 2008 8:13 AM
  • 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.

    Wednesday, July 02, 2008 8:23 AM
  • (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.

    Wednesday, July 02, 2008 9:06 AM
  • ...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

    Friday, July 04, 2008 2:30 PM
  • 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.
     

    Thursday, July 17, 2008 2:28 PM
  • 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)?

    Thursday, July 17, 2008 2:59 PM
  •  If it's nested namespace, please check my reply in this post http://silverlight.net/forums/p/23213/83217.aspx#83217

    Saturday, August 23, 2008 4:36 PM
  •  i just wondered if there's a fix now in the new version for these inheritance problems?

    Wednesday, October 29, 2008 8:39 AM
  • 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.
     
    Tuesday, November 25, 2008 10:25 AM
  • 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")]
    Friday, November 28, 2008 4:13 AM
  •  Thank you very very much

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

     

    thank you!

    Friday, December 05, 2008 10:44 PM
  • 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.

    Saturday, December 06, 2008 12:01 AM
  • 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

    Wednesday, January 21, 2009 9:39 PM
  • 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/
    Friday, March 06, 2009 6:40 AM
  • rapidly, here's my code :

    namespace MyApp
    {

    public class BasePage : UserControl {...}

    public class BaseEditPage<T>: BasePage where T: WCFService.EntityObject, new() {...}

    public class SomethingEdit : BaseEditPage<WCFService.SomethingEO> {...}
    }

    I've no error with this code
    BUT... my xaml don"t compile:

    <my:BaseEditPage x:Class="MyApp.SomethingEdit"
    x:TypeArguments="myWCF:SomethingEO"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    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:my="clr-namespace:MyApp;assembly=MyAppProject"
    xmlns:myWCF="clr-namespace:WCFService;assembly=MyAppProject">

    <my:BaseEditPage>

    some supplenet info:
    the WCFService represent the service WCF that is link to my project and SomethingXXX is class with [DataContract] attribute.

    x:TypeArgument is about to specified the Generic Class in the UserControl xaml !

    this don't run cause the file SomethingEdit.g.cs (the hidden file partial class) don't generate with the normal generic specification like this: SomethingEdit<WCFService.SomethingEO> !!


    any help will be very great !!!

    to read you,
    Patrice

    Tuesday, December 08, 2009 8:36 AM
  •  

    Gees, it is 2010 now, and I am still getting the same bug in Silverlight 4.Yell

    Anyone know any update on this known issue?

    Tuesday, July 13, 2010 11:59 PM
  • I am doing this in windows phone 7.5 developing. didn't touch AsssemblyInfo.cs. I created the derived usercontrol xaml this way:

    <dialogs:BaseUserControl x:Class="......"

    and inserted one line:

        xmlns:dialogs="clr-namespace:<your base usercontrol class path>"

    Everything is fine, but got warning like:

    Warning    1    '....LayoutRoot' hides inherited member '<base usercontrol class>.LayoutRoot'. Use the new keyword if hiding was intended...

    Because in the base usercontrol has defined LayoutRoot, and the automatically created .g.i.cs of the derived usercontrol defines it again.

    Since it's a warning, the app is working fine, I didn't notice any problem so far, will see how it goes. Hope MS can improve the way the g.i.cs creates when the user control is inherited from another usercontrol but not UserControl. I am sure in g.i.cs only needs handling the new xaml elements in the derived usercontrol.

    Good luck!

    Frank

    Wednesday, August 22, 2012 11:56 AM