Page view counter

Did You Know That... you can create Silverlight objects programmatically?

In both Silverlight 1.0 and Silverlight 2.0 it is possible to create Silverlight objects programmatically, in the former using JavaScript and in the latter using, for example, C#.

Creating objects programmatically in Silverlight 1.0 is non-trivial, as demonstrated in this  How Do I video.

Creating objects programmatically in Silverlight 2.0 is far easier, because each XAML object is represented by a CLR object which you can instantiate in whatever language you are using, for example C#.

Ellipse myEllipse = new Ellipse();
parentCanvas.Children.Add(myEllipse);
myEllipse.Width = 120;
myEllipse.Height = 50;
myEllipse.SetValue(LeftProperty, 150);
myEllipse.SetValue(TopProperty, 10);
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = new SolidColorBrush(Colors.Blue);

An early demonstration of dynamic instantiation of Silverlight objects is shown in this How Do I video.

I recently received an inquiry from a developer asking about the relative merits of declaring Silverlight objects in XAML versus doing so programmatically.  Some of the developers in his organization were arguing that it was better to declare all of the objects programmatically "so that they can have more control."

I responded that the point of instantiating Silverlight objects in code-behind is that you can create these objects in response to events that occur while your program is running. The disadvantage is that it is much more difficult to create or manipulate your objects with tools such as Expression Designer, and I'm not convinced that you give up any level of control by creating theobjects at design time.

Fortunately, you are not forced into an either/or choice; you can create your objects using a design tool and then, at runtime,  either manipulate those objects or create new objects in response to events.

Personally, I would resist the idea of creating every object programmatically, as I suspect that the medium to long-term maintenance costs would be very high, as you would be unnecessarily surrendering the advantages of utilizing powerful design tools; but of course this is a generalization  that would have to be evaluated for each particular project.

kick it on DotNetKicks.com
Published 07 January 2008 10:00 AM by jesseliberty

Comments

# jackbond said on 07 January, 2008 05:27 PM

For developers arguing for all objects to be declared via code, I'd ask them if in their web pages contained any html, or if they declared all their objects in javascript. There seems to be some animosity/confusion as to role XAML plays in Silverlight/WPF apps. Not sure why.

# webm0nk3y said on 07 January, 2008 09:21 PM

I'm not sure about 'animosity' but on this matter I have nothing but praise for Microsoft. The development environment is practically perfect.

I have Expression Blend 2 open (on one monitor) and I have my solution open in vs2008 (on another monitor). The solution has my silverlight component project with it's xaml and codebehind and also a silverlight web project which has a reference to the component's assembly so I can test it. Of course this works great when you have multiple monitors.

# DotNetKicks.com said on 07 January, 2008 10:37 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# feddas said on 05 May, 2008 11:58 AM

I was working through and up to the dynamic Controls in the tutorial at silverlight.net/.../controls.aspx .

I wanted to make the dynamically created controls draggable.  I thought this would be easy, I'd simply adjust the dynamically wired events.  Yet the events do not fire at all.  Does Silverlight not yet allow MouseButtonEvents to be dynamically wired?

void Another_Click(object sender, RoutedEventArgs e)

{

Button b = new Button();

b.Content = "I live!";

b.Width = 100;

b.Height = 50;

//the following line was not commented out in the original

//b.Click += new RoutedEventHandler(new_button_Click);

//the following 3 lines were added to make the button draggable

b.MouseLeftButtonDown += new MouseButtonEventHandler(CompositeButton_MouseLeftButtonDown);

b.MouseMove += new MouseEventHandler(CompositeButton_MouseMove);

b.MouseLeftButtonUp += new MouseButtonEventHandler(CompositeButton_MouseLeftButtonUp);

LayoutRoot.Children.Add(b);

}

# erikliffner said on 16 May, 2008 02:41 AM

I have tried the same thing as feddas but without any luck. Is it possible to do this?

# re: Did You Know That… you can create Silverlight objects … | My Geek Solutions said on 16 May, 2008 04:57 AM

Pingback from  re: Did You Know That… you can create Silverlight objects … | My Geek Solutions