Silverlight 2.0 beta 1 is alive – how to use it now?

6 במרץ 2008

תגובה אחת

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/silverlight-20-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 class MyCollection : ObservableCollection<MyObject>
    {
        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.

xmlns:l="clr-namespace:SilverlightApplication1"
    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

<Grid x:Name="LayoutRoot" Background="White">
            <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

<UserControl.Resources>
        <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 x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

Next, put StackPanel and two buttons inside it.

<StackPanel Grid.Row="1" >
            <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

private void Button_Click(object sender, RoutedEventArgs e)
        {
            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…

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

תגובה אחת

  1. אוהד אסטון6 במרץ 2008 ב 13:11

    Oh, I can't wait to the release…

    הגב