Silverlight 2.0 beta 1 is alive – how to use it now?
As you, probably, know, Silverlight 2.0 got “non-commercial” go-live and how you can start using it for real. But how to do it? Let try to understand.
First of all, you have to remove all previous versions of Silverlight as well as remove Silverlight Alpha tools for Visual Studio. Then, install Runtime andm using chain installer, get Visual Studio 2008 tools for Silverlight 2.0 beta 1. I installs SDK, but does not install documentation. You can get it offline here. If everything went well, you’ll have version 2.0.30226.2 installed. As well as inside Visual Studio 2008, you’ll get new project type, named Silverlight.

Now, when we have the installation, our next step will be to understand changes in syntax and build one sample program.
First thing, we’ll do will be creation of DependencyObject.
public class MyObject : DependencyObject {
public string Title {
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); } }
public static readonly DependencyProperty TitleProperty
= DependencyProperty.Register(“Title”, typeof(string),
typeof(MyObject), OnTitlePropertyChanged);static void OnTitlePropertyChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) { }
}
So far so good, but if you’ll try to compile it, you’ll get following error: “Error 1 The type ‘System.Windows.DependencyObject’ has no constructors defined“. What is it? This is beta! In Silverlight 2.0 beta 1, the only object you may directly derrive from is UserControl. So, let’s change out MyObject class to inherit from UserControl. Not very nice, but we have no chance.
Now the turn of collection. We can derrive from IList<T> or ICollection<T>, so let’s write some code.
{
public MyCollection()
{
for (int i = 0; i < 10; i++)
{
MyObject o = new MyObject();
o.Title = string.Format(“Item {0}”, i);
base.Add(o);
}
}
}
You should also put it into resources, so inside you Page1.xaml, we’ll add following lines.
…
Width=”400″ Height=”300″>
<UserControl.Resources>
<l:MyCollection x:Key=”collection”/>
</UserControl.Resources>
Dont’t it looks like WPF? It really is! Let’s see if binding works the same way as in WPF
<ListBox ItemsSource=”{Binding Source={StaticResource collection}}”/>
</Grid>
Compile and run – see nothing? There is a reason for it. If you’ll change the collection to well-known type collection (e.g. string), you’ll see the result. Now you see nothing, ‘cos you have no DataTemplate (for some reason reflected type does not appear as well – nu, shojn, beta
). So, now we know, that we need DataTemplate. Let’s do it
<l:MyCollection x:Key=”collection”/>
<DataTemplate x:Key=”template”>
<TextBlock Text=”{Binding Title}”/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name=”LayoutRoot” Background=”White”>
<ListBox
ItemTemplate=”{StaticResource template}”
ItemsSource=”{Binding Source={StaticResource collection}}”/>
</Grid>
Very well. We have simple ListBox, binded to underlying data source. As alternative, you can use DisplayMemberPath=”Title” (in our case) to visualize simple data item

So far, so good. Let’s add buttons and handlers to our sample program.
First let’s create markup for the root grid
<Grid.RowDefinitions>
<RowDefinition Height=”*”/>
<RowDefinition Height=”*”/>
</Grid.RowDefinitions>
Next, put StackPanel and two buttons inside it.
<Button x:Name=”addButton” Content=”Add Item” Click=”Button_Click”/>
<Button x:Name=”removeButton” Content=”Remove Item” Click=”Button_Click”/>
</StackPanel>
And at the end, handlers for those buttons
{
MyCollection coll = Resources["collection"] as MyCollection;
Button b = sender as Button;
if (b.Name == “addButton”)
{
MyObject o = new MyObject();
o.Title = string.Format(“Item {0}”, coll.Count);
coll.Insert(0,o);
}
else if (b.Name == “removeButton”)
{
if (coll.Count > 0)
{
coll.RemoveAt(0);
}
}
}
Well, it’s absolutely WPF stuff, in spite of the fact, that we are in Silverlight. However, it’s not 100% right. We’re still missing DataTriggers, some of layout controls, style trigger etc. But it’s still in beta, and by now it’s looking like in very near future we’ll be able to develop the same code and just compile it for different platforms: WPF, WPF XBAP, Silverlight (as for me WPF/E is better name for this version, due to fact, that it’s tool close to WPF)
Thank you and be good people. Stay tuned for upcoming articles…
March 5th, 2008 · Comments (2)
2 Responses to “Silverlight 2.0 beta 1 is alive – how to use it now?”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard





January 1st, 2009 at 12:40 am
Do you remember, that we have "go-live" for Silverlight 2.0 and already have build machines
January 1st, 2009 at 12:40 am
Oh, I can't wait to the release…