Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Extending Canvas,Rectangle Objects RSS

8 replies

Last post Nov 16, 2007 07:46 AM by Yi-Lun Luo - MSFT

(0)
  • BigTundra

    BigTundra

    Member

    48 Points

    36 Posts

    Extending Canvas,Rectangle Objects

    Nov 14, 2007 11:53 PM | LINK

    Is it possible to extend the Canvas and the Rectangle objects to include additional properties?  For example Canvas doesn't have a text property should I be able to do something like below to extend the Canvas object.  I've tried this and it compiles, but when I try to run the silverlight page it comes back with a parse error.  I'm not trying to render the text or anything I just want to be able to access it in the codebehind.

     

    public interface ICustomCanvas
        {
            string Text
            {
                get;
                set;
            }
        }
        public class XCanvas : Canvas, ICustomCanvas
        {
            public XCanvas():base()
            {
            }
            #region ICustomCanvas Members
            private string _Text;
            string ICustomCanvas.Text

            {
                get
                {
                    return _Text;
                }
                set
                {
                    _Text = value;
                }
            }

            #endregion
        }

     

    And then here is the Xaml to display the newly extended Canvas:

    <Canvas x:Name="parentCanvas"
            xmlns="http://schemas.microsoft.com/client/2007"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Class="Poc.Page;assembly=ClientBin/Poc.dll"
            xmlns:CustomControls="clr-namespace:Silverlight.Business;assembly=ClientBin/Silverlight.Business.dll"
            Width="640"
            Height="480"
            Background="White"
            >


      <CustomControls:XCanvas Width="144" Height="33" Canvas.Left="100" Stroke="Black" StrokeThickness="2" Canvas.Top="100" Text="MyCanvas">
       
      </CustomControls:XCanvas>
     
    </Canvas>

  • Psychlist1972

    Psychlist1972

    Contributor

    6802 Points

    1079 Posts

    Microsoft

    Moderator

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 01:55 AM | LINK

    You are implementing the interface explicitly, which means the XCanvas would need to be cast to ICustomCanvas before you would see that property

    Instead, change it to either not use an interface, or to use the interface but expose the property like this:

          public string Text { ... }

    Hope that helps. 

    Pete

    Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
    10rem.net - Pete Brown's site and blog | twitter: @pete_brown
    I work for the Developer Guidance group in Microsoft. Opinions are my own.
  • gwynn

    gwynn

    Member

    178 Points

    30 Posts

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 02:29 AM | LINK

    Hi,

    At this time, I don't believe it is possible to extend the built in elements such as Canvas or Rectangle by inheriting from these classes and adding properties.(At least, I haven't seen any examples of this done.)

    Instead, you can create a custom control (by inheriting from the Control class) and create a Canvas object internally. While you can enhance your control so that it acts very similar to a canvas - it still won't be a Canvas object as it doesn't inherit from this class.
     

    Cheers,
    Gwynn

    --------------------------
    Gwynn Kruger
    http://blog.silverspud.com
    --------------------------
  • Psychlist1972

    Psychlist1972

    Contributor

    6802 Points

    1079 Posts

    Microsoft

    Moderator

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 02:52 AM | LINK

    You can, actually, I just tried it to make sure.

    <Canvas x:Name="parentCanvas"
            xmlns="http://schemas.microsoft.com/client/2007" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:c="clr-namespace:PeteBrown.SilverlightCanvasTest;assembly=ClientBin/PeteBrown.SilverlightCanvasTest.dll"
            Loaded="Page_Loaded" 
            x:Class="PeteBrown.SilverlightCanvasTest.Page;assembly=ClientBin/PeteBrown.SilverlightCanvasTest.dll"
            Width="640"
            Height="480"
            Background="White"
            >
    
      <c:XCanvas Text="Foo" Width="100" Height="100">
        <TextBlock Text="Some TextBlock Inside XCanvas" />
      </c:XCanvas>
    </Canvas>
    

    And here is the code

        public class XCanvas : Canvas
        {
            private string _text;
    
            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }
    
        }
    

     

    The main limitation we have right now is animation. Since we can't create our own dependency properties, we can't animate any of the properties we add. 

    Pete

    Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
    10rem.net - Pete Brown's site and blog | twitter: @pete_brown
    I work for the Developer Guidance group in Microsoft. Opinions are my own.
  • BigTundra

    BigTundra

    Member

    48 Points

    36 Posts

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 06:07 PM | LINK

    Thanks for the help everyone.  I have been able to extend the Canvas as Pete said, I guess I didn't need the added layer of the interface. 

     

    Although, the problem currently is that the Rectangle is a sealed class so you can't extend it.  Does anyone know if that is something that will change when Silverlight 1.1 goes to Beta or if that is going to remain sealed.  If it is going to remain sealed does anyone know a workaround.  The only way I can see to workaround it is to create a user control that is a rectangle, but then I would have to handle all of the xaml tags that can be passed in to a rectangle manually.  I would like to avoid that, but I guess it's an option.

    extending rectangle

  • jasonxz

    jasonxz

    Participant

    1787 Points

    557 Posts

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 06:14 PM | LINK

    You're best bet might be to create a custom Canvas class that contains a Rectangle object and expose the Rectangle's properties by creating wrappers in the custom Canvas.

  • gwynn

    gwynn

    Member

    178 Points

    30 Posts

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 06:40 PM | LINK

    Great stuff Pete!

    I'll give this a shot tonight. I tried earlier but ran into errors but I'll give your example a go.

    Cheers,
    Gwynn

    --------------------------
    Gwynn Kruger
    http://blog.silverspud.com
    --------------------------
  • BigTundra

    BigTundra

    Member

    48 Points

    36 Posts

    Re: Extending Canvas,Rectangle Objects

    Nov 15, 2007 10:33 PM | LINK

    Jason,

     Your right I wanted to save that for the last resort. 

    I did notice that the shape object can be extended and the only difference between the shape and the rectangle are what is in the rectangle constructor and the radiusX radiusY properties.  Anyone have the code for that :)

  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: Extending Canvas,Rectangle Objects

    Nov 16, 2007 07:46 AM | LINK

     

    Unfortunately, you won't be able to extend Rectangle, or any of the concrete shapes. These classes uses an internal code to determine what to draw on the screen. They pass this information in the Constructor. For example, here's Rectangle's code dumped by Reflector:

    public Rectangle() : base(0x5f)
        {
        }

     

    The base(uint) method is an internal method. So you won't be able to call it in your own application. If you omit this information, nothing will get drawn. You'll have to use Jason's suggestion...

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.