SilverLight custom control
Last post 07-28-2008 7:40 PM by BigDubb. 15 replies.
Sort Posts:
05-13-2008 3:54 PM
SilverLight custom control

how can we create a SL custom control. we cant derive from frameworkelement it has an internal Ctor. also if its possible we can not override the OnRender method. I know its not possible now but I need to know if this will gonna be supported in future or not.

 

also is there any workaaround to create my own custom control. I need to do an Image control with extra properties.

Sam Anderson

Loading...
Joined on 03-22-2008
Posts 7
05-13-2008 8:29 PM
Re: SilverLight custom control

 public class ImageControl : Control

{

}

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,100
05-14-2008 5:33 AM
Re: SilverLight custom control

and then what. I need to override the OnRender method. I need to draw Frames arround the image. also I need to change the behaviour that Image control has.

Sam Anderson

Loading...
Joined on 03-22-2008
Posts 7
05-14-2008 6:07 AM
Re: SilverLight custom control

Please check this quickstart to know how to create a Silverlight 2 custom control: http://silverlight.net/Quickstarts/BuildUi/CustomControl.aspx

Hope this helps,
Jim (http://jimmangaly.blogspot.com/)

Please MARK the replies as answers if they answered your question

http://www.identitymine.com/

Jim Mangaly

Loading...
Joined on 04-21-2008
Kochi, India
Posts 367
05-14-2008 9:30 AM
Re: SilverLight custom control

Sam Anderson:

and then what. I need to override the OnRender method. I need to draw Frames arround the image. also I need to change the behaviour that Image control has.

 

There is no OnRender method in silverlight control you can override. Take a look at the link Jim pointed you should be able to create a custom control in silverlight.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,100
05-14-2008 12:16 PM
Re: SilverLight custom control
I need to know if it will be supported in future release or not.if yes we can wait to release our products. if not we will live with it.
Sam Anderson

Loading...
Joined on 03-22-2008
Posts 7
06-23-2008 10:52 PM
Re: SilverLight custom control

 I've made a custom control based on Button and am getting this error at runtime:

 Additional information: AG_E_PARSER_UNKNOWN_TYPE

 If I mouse over the control in xaml I'm seeing:

 Type 'MyButton' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.   


So I found this post and the CustomControl tutorial says it's now obsolete. I'm assuming that's just the tutorial, not custom controls. Is there another tutorial around that will show me what I've done wrong? I've done this before. I'm sure its something simple?

Any ideas?

Stephen

Lyynx

Loading...
Joined on 04-26-2008
Perth, Australia
Posts 11
06-23-2008 11:16 PM
Re: SilverLight custom control

Here is another one on how to create CustomControl:

 http://www.codeproject.com/KB/silverlight/mediabutton.aspx

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 2,100
06-23-2008 11:57 PM
Re: SilverLight custom control

sladapter:

Here is another one on how to create CustomControl:

 http://www.codeproject.com/KB/silverlight/mediabutton.aspx

 

 Thanks for that. I did find that one in my searching, but it didn't help me other than show me that I was doing everything right. Except for one thing. My constructor did not have the public keyword in front of it. 

Took me ages to spot the error! The class was quite happy but my xaml couldn't get to it. I assumed the default was public but I guess it must be something else? protected or private maybe?

cheers,

Stephen

Lyynx

Loading...
Joined on 04-26-2008
Perth, Australia
Posts 11
06-24-2008 12:21 AM
Re: SilverLight custom control

Lyynx:
I assumed the default was public but I guess it must be something else? protected or private maybe?

I think it is internal.

Jim (http://jimmangaly.blogspot.com/)

Please MARK the replies as answers if they answered your question

http://www.identitymine.com/

Jim Mangaly

Loading...
Joined on 04-21-2008
Kochi, India
Posts 367
06-24-2008 8:01 AM
Re: SilverLight custom control

 if you only want to create a control , that can be used as dll, then create a silverlight project with add a user control to it. delete page.xaml

from the bin folder we can get its dll and use it in any other project.

Sachin Mukhija

Software Engineer 

Sachin Mukhija
"Please Mark as answer if answer is correct".

skm.software@yahoo.com

Loading...
Joined on 06-18-2008
Delhi
Posts 32
06-24-2008 9:30 PM
Re: SilverLight custom control

There are no plans to add an OnRender method for this version. There will be no low-level drawing APIs. Anything you want to see on the screen must be done by composition, i.e. if you want a border around a custom Control, then you need to have a Border element (or a Rectangle) in the control's visuals.

Dave Relyea [MSFT]
http://blogs.msdn.com/devdave

Dave Relyea

Loading...
Joined on 05-09-2007
Posts 224
07-26-2008 2:16 AM
Re: Re: SilverLight custom control

Here is another great link to creating a custom control:


http://msdn.microsoft.com/en-us/magazine/cc721611.aspx

richardfen

Loading...
Joined on 07-26-2008
Posts 5
07-26-2008 4:54 AM
Re: SilverLight custom control

 Hi

Please Read whole post it looks lengthy but its helpful.


Go step by step
1. Create New Silverlight Project CustomControlDemo
2. Then Add New ClassLibrary MyClassLibrary
3. Then add Silverlight User Control to MyClassLibrary By
Right Clicking on MyClassLibrary >  Add > New Item
MyCustomControl.xaml rename that like this

4. Then Add Some Object to MyCustomControl.xaml to change the look of control
like this

MyCustomControl.xaml
===========================
<UserControl x:Class="MyClassLibrary.MyCustomControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="150">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Background="#FF5B5B5B">
            <Rectangle HorizontalAlignment="Stretch" Margin="11,31,9,9" VerticalAlignment="Stretch" Fill="#FFFFFFFF" Stroke="#FF000000"/>
            <Button Height="24" HorizontalAlignment="Right" Margin="0,3,13,0" VerticalAlignment="Top" Width="29" Content="X" x:Name="btnClose"/>
            <Button Height="26" HorizontalAlignment="Stretch" Margin="167,0,155,17" VerticalAlignment="Bottom" Content="Ok" x:Name="btnOk"/>
            <TextBlock Height="13" Margin="11,9,199,0" VerticalAlignment="Top" Text="My Custom Control" TextWrapping="Wrap" FontSize="12" HorizontalAlignment="Stretch" Foreground="#FFFFFFFF"/>
            <TextBlock Margin="29,48,33,58" TextAlignment="Center" TextWrapping="Wrap">
                <Run Text="This "/>
                <Run FontSize="14.6666666666667" Text="Demo "/>
                <Run Text="is "/>
                <LineBreak/>
                <Run Text="for How to Develop a Custom Control."/>
            </TextBlock>
        </Grid>

    </Grid>
</UserControl>
 ====================================

5. Right Click on MyClassLibrary and Build Library and then Build Entire Solution By Right Clicking on Solution 'CustomControlDemo'.

6. Then Right Click on Refrences of CustomContolDemo i.e Silverlight Application

7. Then Click on Add Reference..

8. Then go to Second Tab i.e Project. you will find MyClassLibrary There. Select That and Click Ok.
    The Library Wil Get Added to Your Silverlight Application (CustomControlDemo)  from ClassLibrary (MyClassLibrary).

9. Then go Page.xaml.cs and add following code to that i.e Like

Page.xaml.cs
=============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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;
using MyClassLibrary;                     <==== Add this to Add Your Contorl's Library to Your Application


namespace CustomControlDemo
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            LayoutRoot.Loaded += new RoutedEventHandler(LayoutRoot_Loaded);     //<=== Add this Loading event to LayoutRoot
        }

        void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();

            //   Create Object of Class i.e. Creating Control on Page.xaml    
            MyCustomControl  myCustomControl = new MyCustomControl();                          //<=== Add This line to Create Object Of Class from Your Libraray    
            
            // Alignment of Control
            myCustomControl.HorizontalAlignment = HorizontalAlignment.Center;
            myCustomControl.VerticalAlignment = VerticalAlignment.Bottom;

            //    Adding New Childern On Loading of LayoutRoot to Page.xaml
            LayoutRoot.Children.Add(myCustomControl);                //<=== Add This line to Add Your Control to Page.
        }
    }
}

==================================================
 
10. Save Page.xaml.cs

11. Now Press F5 i.e Run the Application

12. You will get Control on Page.xaml


++++++++++++++++++++++++
Other way to do it is
++++++++++++++++++++++++


1. Go to Page.xaml

2. Add Assembly Refrence to Page.xaml like this

Page.xaml
==============================================
<UserControl x:Class="CustomControlDemo.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300"
     xmlns:cc="clr-namespace:MyClassLibrary;assembly=MyClassLibrary">              <!--<====  Add  This Line to Add Your Library for this Application -->
    <Grid x:Name="LayoutRoot" Background="White">
        <cc:MyCustomControl HorizontalAlignment="Center" VerticalAlignment="Top" ></cc:MyCustomControl>   <!-- <==== Add this line to Add Your Custome Control To the Page.xaml -->
    </Grid>
</UserControl>
===========================================

3. Save The Page.xaml

4. Run the Application.


if you have done both then You will find two custom controls on Page.xaml

Upper One form ===> Page.xaml.cs
Lower One from ===> Page.xaml



you can download and run the demo from here

http://www.esnips.com/web/CustomControl

this will help you more.

Mr. Sanjay S. Kadam
(Jr. Software Developer)
[AksTech Solutions Pvt Ltd.]

Mark it as answer if its helpful for you.

sanjay_sk16

Loading...
Joined on 03-01-2008
Thane
Posts 31
07-26-2008 6:32 AM
Re: SilverLight custom control

 You can check out this QuickStart:
How to create a Silverlight custom control

It is one in a series of Silverlight QuickStarts can be found on www.visualwebgui.com/silverlight
 

 


 

rg10

Loading...
Joined on 07-21-2008
Posts 15
07-28-2008 7:40 PM
Re: SilverLight custom control

 *** Clarification Point ***

 Whenever a custom silverlight control is to be added to an ASPX page, that control needs to be implemented as a page.xaml file and included in the XAP file, then each XAP file that houses each control needs to be  added explicitly in the silverlight tag. 

i.e.

MyCustomControl is implemented in Page1.XAML which generates MyCustomControl.XAP file

MyOtherControl is implemented in Page1.XAML which generates MyOtherControl.XAP file.

To impelement in the ASPX mark up you would have

 <asp:ScriptManager ID="MyManager" runat="server"/>

<asp:Silverlight id = "MyControl" runat="server" Source = "~/ClientBin/MyCustomControl.XAP" />

<asp:Silverlight id = "MyOtherControl" runat="server" Source = "~/ClientBin/MyOtherControl.XAP"/>

There is no way to implement a library of associated controls in a .xap file and choose which control to include in an ASPX page correct, one XAP file per Control?  

Secondly, if there are custom events on the control there is no way to capture them on the ASPX.cs file correct? Is the only way these can be captured via JS?

I know if I had a pure Silveright application this would be feasible, but complete migration vs. cherry picking ideal implementation points and gradually migrating over feels like a much more feasable transition for both users and stake holders.  

Thanks.

BigDubb

Loading...
Joined on 06-11-2008
Posts 21
Page view counter