Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Silverlight User Control Inheritance RSS

69 replies

Last post Jul 14, 2010 03:59 AM by Tintin1983

(0)
  • caperaven

    caperaven

    Member

    133 Points

    107 Posts

    Silverlight User Control Inheritance

    Mar 24, 2008 07:19 AM | LINK

    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

    14968 Points

    2799 Posts

    Re: Silverlight User Control Inheritance

    Mar 24, 2008 07:28 AM | LINK

    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

    4973 Points

    947 Posts

    Re: Silverlight User Control Inheritance

    Mar 24, 2008 11:50 AM | LINK

    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
    My blog
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: Silverlight User Control Inheritance

    Mar 24, 2008 12:15 PM | LINK

    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

    4973 Points

    947 Posts

    Re: Silverlight User Control Inheritance

    Mar 24, 2008 12:29 PM | LINK

    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
    My blog
  • mchlSync

    mchlSync

    Star

    14968 Points

    2799 Posts

    Re: Silverlight User Control Inheritance

    Mar 24, 2008 01:57 PM | LINK

    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

    133 Points

    107 Posts

    Re: Re: Silverlight User Control Inheritance

    Mar 25, 2008 06:56 AM | LINK

    Thanks guys, will test it tonight.

  • yourbuddypal

    yourbuddypal

    Member

    198 Points

    85 Posts

    Re: Re: Silverlight User Control Inheritance

    Mar 25, 2008 05:44 PM | LINK

     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

    14968 Points

    2799 Posts

    Re: Re: Silverlight User Control Inheritance

    Mar 26, 2008 01:22 PM | LINK

    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

    316 Points

    125 Posts

    Silverlight User Control Inheritance

    Mar 26, 2008 01:56 PM | LINK

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