Pages

Thursday, April 14, 2011

Silverlight 5 Beta - Lets deal with its features

In MIX 2011, we can see our very own silverlight new version called Silverlight 5 beta is released. Yes, if you are looking for it, its time to download the beta and try them out. The major goal of Silverlight 5 is to move the silverlight development more towards the WPF and hence releasing some of the benefits that you already enjoy being a WPF developer is now available to silverlight. In this post lets talk about the features that were introduced with Silverlight one by one.

To install Silverlight 5

To try the features, you first need to download the Beta. Lets follow the steps to install silverlight in your machine.

  1. You first need to download Visual Studio 2010 (SP 1) if you didn't have done that already. 
  2. After you install Visual studio update you need Silverlight tools for Visual Studio
  3. Optionally you can also try Silverlight help tools to get started.
Now after you are done with this, lets talk about few basic features introduced in Silverlight 5.

Silverlight 5 Features 

You should notice that there are few major benefits that are not present yet with Silverlight 5 beta, to name a few :
  • Our old requirement to support 64 Bit is not present
  • You still cannot launch external applications (using PInvoke) with elevated trust level
  • DataContextChanged is still not present.
  • Vector Printing


Yet there are few things that you can try now. Lets talk about them.

1. Implicit DataTemplates for Control : 

If you are a WPF developer reading this, you might be thinking that it should be already present before, but it isnt. For Silverlight developers there is a new adjustments made to ensure that DataTemplates are loaded based on Type of Data.

Say you own a ListBox with custom DataTemplate as shown below :

<Grid>
    <Grid.Resources>
        <DataTemplate DataType="local:Abhishek">
            <Border Background="Red">
                <TextBlock Text="Abhishek's Template Selected" />
            </Border>
        </DataTemplate>
        <DataTemplate DataType="local:Abhijit">
            <Border Background="Green">
                <TextBlock Text="Abhijit's Template Selected" />
            </Border>
        </DataTemplate>
    </Grid.Resources>

    <ListBox ItemsSource="{Binding ListofPeople}" Grid.Row="0"/>
</Grid>

Here you can see a new property called DataType which has been already present with WPF earlier but recently added with silverlight, will let you specify the type of the object for which the DataTemplate is associated with.

Now if we create the ListofPeople with both objects of Abhishek and Abhijit, it will automatically select the type based on the individual DataType.

public class Model
{
    private List<People> listofPeople;
    public List<People> ListofPeople
    {
        get
        {
            this.listofPeople = this.listofPeople ?? this.LoadListofPeople();
            return this.listofPeople;
        }
    }

    private List<People> LoadListofPeople()
    {
        List<People> peop = new List<People>();
        peop.Add(new Abhishek());
        peop.Add(new Abhijit());
        peop.Add(new Abhishek());
        return peop;
    }
}

If you force the Model into DataContext, it will choose DataTemplate according to the type passed.

2. AncestorType in RelativeSource

WPF has a facility to search the AncestorType for a control from a child control such that it will find the next parent control that matches the Ancestor Type defined. Silverlight implements this feature to enable you to search your ancestor from child control from your visual tree and bind the child property with it.  Lets take a look into it.

<DataTemplate>
    <TextBox Width="{Binding Width RelativeSource={RelativeSource AncestorType=Border, AncestorLevel=5}} />
</DataTemplate>

Say you have a TextBox defined within the DataTemplate and you want to bind the Width of the TextBox to the width of the control somewhere outside it, you can use RelativeSource Markup extension to find it from the Visual tree.

3. Binding in Style Setters

Silverlight introduces Binding in style Setters. Hence you can use Binding, StaticResource or any markup extension inside the style Setters.

Let you have a class that defines your resource based on some custom logic :

public class MySettings 
{
    public Thickness Margin {get;set;}
}

Now from your style setters you can easily point to the class to get the value of the Thickness.

<local:MySettings x:Key="msettings" />
<Style>
    <Setter Property="Margin" Value="{Binding MyMargin, Source={StaticResource msettings}}" />
</Style>

In the code, it will bind the msettings object and pull the data from MyMargin.


4. ComboBox AutoSelection while Typing

Silverlight introduces a new feature for its existing ComboBox to auto - discover items from the list while the user types text. It has a new property called DisplayMemberPath where Text should be placed which needs to be searched. If you have ever worked with ASP.NET, it would be very easy to grab. 

<ComboBox DisplayMemberPath="YourMember" ItemsSource="{Binding MyItems}" />

The DisplayMemberPath will automatically find YourMember as property from the list of objects associated in MyItems.

Note : To work better, MyItems list should be sorted.

5. RichTextBoxOverflow Control : 

One of the interesting control that was introduced with Silverlight today is the RichTextBoxOverflow control. This control will show up the portion of the RichTextbox which is overflown to it. A special property called OverflowContentTarget is added to the RichTextBox control to specify the Overflow control.

<RichTextBox OverflowContentTarget="{Binding MyOverflowControl}" />
<RichTextBoxOverflow x:Name="MyOverflowControl" 
                     OverflowContentTarget="{Binding MyOverflowControl2}"/>
<RichTextBoxOverflow x:Name="MyOverflowControl2" />

From the code, you will see the First RichTextBox overflows automatically to he overflow control and eventually both control behaves one homogeneous control. It allows you to select the portion overflown to other control and hence these control is actually an option to show up the portion that is not viewed in the original RichTextBox. Cool na.

6. Support for 3D Hardware Accelerated Graphics.

Silverlight now supports 3D Hardware acceleration. 3D graphcs acceleration can be enabled for your Silverlight object using EnableGPUAcceleartation  from the Params of Object tag. Once you enable it, you can use GraphicsDevice in your system to draw 3D objects on the DrawingSurface.


Even though there are lot more changes to talk about, but I want to leave them for some other time.

If you are dying to know all of them, get into "Silverlight 5.0 features" to read more.

Conclusion

The primary goal of this release is to lead close parity between existing silverlight with WPF development and hence giving better developer experience by better tooling support and line of business. I am sure you would love to play around with it.

Thank you for reading.

No comments:

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.