learn more about riagenic
14 May 2007

The basics of a Silverlight Control

I've been sitting here for the past few days really getting my head deeper into Silverlight and I'll post more example's of going from Flex to Silverlight later this week. I will however branch out a bit here and illustrate one nugget that I wish someone had of sat over my shoulder and told me about prior to embarking on Silverlight 101.

InitializeFromXaml is your friend.

In C# (Managed Code) I was struggling initially on how the hell do I create children within a Canvas tag for example. In that how does Orcas know how to assemble the pieces together so that I have not only nested children via C# but at the same time, ensuring Silverlight runtime knows as well (ok, one in the same I guess).

How about some code:

XamlPath = "com.mossyblog.MyFile.xaml";
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream(XamlPath);
actualControl = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

A bunch of stuff happens here in 2 small lines of code, but basically it reads in the file path I pass in (XamlPath) and invokes this.InitializeFromXaml() to effectively read in the file and instantiate this control.

It's important to make mental note of this as when you write your own controls inside Silverlight, you will need to do this for all Controls (unless you extend Canvas or something along those lines).

That being said if you use the example UI Framework (Located in the Alpha 1.1 SDK) you can effectively bypass a lot of this by simply extending the ControlBase Class (which does it's own automatic lookup in terms of path finding and appropriate level of instantiation)

 Creating Children from within a Custom Control

After you master the "How" for bringing in XAML files as controls within your code-behinds, it's now a bit of a head scratching exercise on how one is able to then instantiate sub-children within?

If you do a "this." and await intelli-sense you're not going to see any methods like "add()" which is what you need right?

Well again, looking at the SDK examples you will note the following in some parts:

((Canvas)ActualControl).Children.Insert(0, content);
[via ListBox.cs]

Inside ControlBase class, there is a public property named AcutalControl, which then points to a private property called actualPath which was given it's value based off the result of this.initializeFromXaml() method invocation (Important to note kids).

This specific type returned from doing this is FrameworkElement (which basically means all roads lead back to this guy). Now, FrameworkElement does have  Add children methods if you were to read-up on it via the XAML documentation (WPF folks will love it). In Silverlight however, if you were to typecast it as a Canvas element (which is legal) you effectively are then able to use Children.Add( myChild ) approach to life.

Example:

public VideoThumbNail()
{
    this.setXamlPath("APAC07.Carrousel.VideoThumbNail.xaml");   
    this.createChildren();
    this.measure();
    this.updateDisplayList();
}

public void createChildren() {
    base.createChildren();

    ImageThumbNail it = new ImageThumbNail();
    ((Canvas)ActualControl).Children.Add(it);

}

I wrote my own event management approach to Silverlight simply because I was bored and thought it may help Adobe Flex Developers understand Silverlight by using similar concepts found within Adobe Flex 2.0.1 framework.

I'm not kidding, with enough time I think I could easily code-port the framework into Silverlight (given Adobe Flex soon will be open source - it may even be legal! hehe).

I hope this helps folks understand how the creation of children happens with managed code inside Silverlight development space. It's pretty straightforward once someone shows you at the start and then from there on out it's just downhill all the way (not as scary).

Back to coding, as I need this demo finished by Wednesday for the APAC Sharepoint conference where I'll be showing folks how Sharepoint + Silverlight can play a role with one another.

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Steven Nagy said:

I'm using InitializeFromXaml but the approach above means that the XAML file is built into the DLL right? So what if the designer wants to make a change to the Xaml and see their results? They need to build. (Yeah I know Blend can build). Would it be better to find a way to find the Xaml at runtime?

13 May 07 at 5:55 PM
# Mike Harsh's Blog said:

Scott has a great post about the things he learned while creating his first Silverlight control. He’s

15 May 07 at 10:43 AM
# East said:

Scott 在他的博客上给出了一个实际创建Silverlight Controls的过程,值得参考。 http://blogs.msdn.com/msmossyblog/archive/2007/05/14/the-basics-of-a-silverlight-control.asp

15 May 07 at 12:20 PM
# WynApse said:

Interesting Silverlight posts today: Silverlight Control

15 May 07 at 5:24 PM
# mystery said:

Hi,

I am new to Silverlight and would like to ask you to give me some tutorial for step by steb building Silverlight controls from the very beginning.

Thanks in advance.

16 May 07 at 10:18 AM
# John Mandia's Points of Interest said:

Another week has shot by. Didn't have any time to look at Silverlight although I have managed to download

20 May 07 at 8:17 AM
# Community Blogs said:

Another week has shot by. Didn't have any time to look at Silverlight although I have managed to

20 May 07 at 8:18 AM
# Community Blogs said:

Another week has shot by. Didn't have any time to look at Silverlight although I have managed to download

20 May 07 at 8:50 AM
# Ezequiel Jadib said:

Microsoft Silverlight is a cross-browser , cross-platform plug-in for delivering the next generation

21 May 07 at 11:53 AM
# Michael's Blog said:

During the weekend I spent some minutes to collect some of the greatest Silverlight examples. Most of

05 June 07 at 3:17 AM
# Chris Bowen's Blog said:

Though the jury is still out on what controls we'll see in or alongside Silverlight 1.1 when it's released,

30 July 07 at 12:14 PM
# Noticias externas said:

Though the jury is still out on what controls we'll see in or alongside Silverlight 1.1 when it's

30 July 07 at 12:30 PM

Leave a Comment

Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About scbarnes

Scott Barnes currently is a Rich Platform Product Manager (WPF & Silverlight). He has been working with Adobe/Macromedia technology for the past 10 years with a main focus specifically on Internet Applications (aka. RIA, Rich Client Technology etc).

Scott first started out as a graphic designer in the late 90’s and over the years developed a passion for programmatic art (Designer + Developer mind). He recently has branched out further into 3D modelling and animation making full use of both his designer + developer mindset.

"..The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man..." - George Bernard Shaw
Page view tracker