Wednesday, October 29, 2008

Implicit Styles in Silverlight

WPF supports both explicit and implicit styles (application wide styles), while Silverlight supports only explicit styles. This means that in Silverlight when you define a style as a resource, you have to give it an explicit key, and call that key wherever you want the style to be applied. For example, if you have defined a TextBlock style in App.Resources, you have to explicity set the Style property on all TextBlocks within the application like this:

<TextBlock Text="Text1" Style="{StaticResource key1}"/>

<TextBlock Text="Text2" Style="{StaticResource key1}"/>

<TextBlock Text="Text3" Style="{StaticResource key1}"/>



This has been an issue Silverlight developers have been grappling with for a long time. The Silverlight 2 release (RTW) did not have a solution for this. Fortunately, the Silverlight Toolkit that has been released yesterday, comes with an ImplicitStyleManager that makes implicit styles possible in Silverlight. If you are not familiar with the Silverlight Toolkit, you can read about it here.

So, how do you make use of this?

1) Download the Silverlight Toolkit.

2) Copy Microsoft.Windows.Controls.Theming.dll that comes within the toolkit to your Silverlight project and add a reference to it.

3) Add a reference to the namespace within which the ImplicitStyleManager resides:

xmlns:theming="clr-namespace:Microsoft.Windows.Controls.Theming; assembly=Microsoft.Windows.Controls.Theming"


4) Now use the ApplyMode property to set implicit styling:

<StackPanel x:Name="stackPanel" theming:ImplicitStyleManager.ApplyMode="OneTime">

<TextBlock Text="Static Text 1" />

<TextBlock Text="Static Text 2" />

<StackPanel>


5) Of course, you should have defined the style somewhere in the resources collection as shown below:

<Application.Resources>

<Style TargetType="TextBlock">

<Setter Property="Foreground" Value="Red" />

<Setter Property="FontSize" Value="20" />

<Setter Property="Margin" Value="5" />

<Style>

<Application.Resources>


Now you are good to go. The ImplicitStyleManager will ensure that the style is passed down to the TextBlock down the element tree.

If you dynamically add a TextBlock to the element tree (at run time), you will find that the TextBlock will not get the style. In this case, you will have to set ImplicitStyleManager.ApplyMode="Auto". But setting it to Auto can have a major performance impact, so use this sparingly.

There you go - implicit styles in Silverlight :).

Saturday, September 27, 2008

Silverlight 2 RCO is Out

Silverlight 2 RCO is Out! So that means that the Silverlight RTW (Release To Web) is round the corner. The RCO release is a developer-only runtime edition that will help developers to prepare their existing Silverlight 2 Beta 2 applications for the final release. Some points to note:
  • This RCO release is feature complete - so do not expect any more controls/features in the Silverlight 2 release. However more controls/features can be expected in future months to be built on top of Silverlight 2 (these will not be part of the core runtime).

  • Three new controls added - ComboBox, ProgressBar, and PasswordBox. These along with most other controls will be part of the Silverlight runtime (which means that these will not be needed to be downloaded with the Silverlight application).

  • The Silverlight runtime is still only 4.6MB in size.

  • The default control styles/templates have been modified.

  • Once Silverlight 2 is released, users with existing beta releases installed will automatically be upgraded to use the final version (also developer machines with RCO , I suppose).

  • A modal dialog MessageBox is now available.

  • System.Windows.Controls.Extended.dll renamed to System.Windows.Controls.dll

  • If you are using custom controls, remember that generic.xaml is now expected in the 'themes' folder rather than at the root of the project.

Saturday, August 30, 2008

Silverlight on the Desktop

Its been almost two months since I blogged - just haven't had the time. But tonight I just couldn't resist making this post. My brilliant colleague at IdentityMine, Sameer Thiruthikad has been spearheading work on an exciting Silverlight utility called Desklighter, and the Beta is now public.

What is the Desklighter? Well, ever wished you could package your Silverlight application into an stand alone executable that could work offline? That is precisely what the Desklighter enables you to do. Read the whole story here, and be sure to try it!

Wednesday, June 4, 2008

What's New In Silverlight 2 Beta 2?

Silverlight 2 Beta 2 was announced in TechEd earlier this week. The actual bits are not yet public, but I think it should be soon.
(Update: The Beta 2 bits are now available. Scott Gu has a detailed list of changes here)

The best news for me was that there are several changes for making the Silverlight model as close as possible to the WPF model. This is very important for Silverlight to live up to its reputation of being a WPF subset (remember its code name WPF Everywhere? )

Below are some changes:

  1. Improved Templating model using the Visual State Manager
  2. New Tab Control
  3. DataGrid improvements: Autosize, Reorder, Sort, Performance increase
  4. TextBox: Text wrapping and multiline selection highlighting in textbox.
  5. Includes the Visual Tree Helper.
  6. DataBinding: Binding to attached properties is now supported.
  7. Controls like the DataGrid which are now not part of the runtime (and have to be downloaded as part of the XAP) have been moved to the runtime. This is good news, but then the XAP is going to be heavier :).
  8. DeepZoom: XML based file format
  9. Full Screen Mode keyboard support: The only key supported earlier was escape. This was supposed to be for security reasons. Now additional keys are supported (arrow, tab, enter, home, end, pageup/pagedown, space).
  10. A bunch of new stuff on the Networking side: Upload support for WebClient, ability to allow user to increase Isolated Storage capacity (via UI), duplex communications ("push" from Server to client), new VS template for “Silverlight-enabled WCF Service”.
To see a full list of changes see this post.

Tuesday, June 3, 2008

Microsoft Innovation Days, Kochi


Microsoft India has been holding events around the country to get people excited about some of the newer technologies. Last week, an event was held at Kochi (Cochin) called Microsoft Innovation Days. One of the sessions was on Silverlight. Since IdentityMine has been into this technology for more than a year, we were invited to present a 30 minute part. I did the presentation on behalf of IdentityMine, and it was a great experience talking to the 200 odd developer crowd!

During the presentation, I showed a demo of a Silverlight 2 application we created. This was an application (not a full fledged one) that was created in just about a week's time. The short time period surprised a lot of people!

I discussed briefly some of the features used in the Silverlight demo - like Databinding, Templates, Animations, Drag and Drop, Video Brush, Isolated Storage, and Video Markers. I also shared some of our experiences working with Silverlight, including the Developer-Designer workflow. My session ended with a fun example of using templates - I had ported the 'Button Fish' (of Avalon Feature Fest fame) to Silverlight

I think we had a lot of people excited about Silverlight. Many in the audience later expressed their desire to try out Silverlight.

Sunday, May 11, 2008

Assemblies in the Silverlight Runtime

Note:
  • Details in this post apply to Silverlight 2 Beta 1, and may change in future Silverlight versions.
  • You may want to read my previous post to get a better understanding this post.

Say you have a Silverlight application with a Canvas and a ListBox. Both these controls belong to the same namespace System.Windows.Controls. Will these controls be downloaded as part of this particular application's xap, or will this be part of the Silverlight runtime installed initially by the user?

The answer is interesting: As far as Silverlight 2 Beta 1 is concerned, the Canvas is within the System.Windows.dll which is part of the Silverlight runtime and hence will not be downloaded to the client along with the xap. However, the ListBox is within the System.Windows.Controls.dll which is not included in the Silverlight runtime. So this will be downloaded with the xap package.

This also means that if you are creating a custom splash screen then you can use the Canvas within it, but not the ListBox since the splash screen will show before the xap has been downloaded.

Friday, May 9, 2008

Namespaces and Assembly Names

Note: Details in this post apply to Silverlight 2 Beta 1, and may change in future Silverlight versions. The basic idea though remains the same.

A Silverlight developer recently reported this problem. He wanted to use the Silverlight DataGrid in his application. He knew that the DataGrid is a part of the assembly System.Windows.Controls.Data.dll. So he added a reference to this assembly in his Silverlight project. So far so good.

Next he tried to import the namespace by typing in "using System.Windows.Controls.Data" so that he could start using the DataGrid. Now here started the problem. Visual Studio refused to recognize this namespace. Was this a bug in Silverlight, or perhaps in Visual Studio?

Neither. It is true that the DataGrid resides in the assembly System.Windows.Controls.Data.dll; but it is still part of the SystemWindows.Controls namespace just like the other controls in Silverlight.

Let us look at some other examples. The TextBlock, Canvas and Grid too belong to the System.Windows.Controls namespace but they actually reside in the assembly SystemWindows.dll not in SystemWindows.Controls.dll.

By the way, the ListBox is one control that belongs to the namespace System.Windows.Controls and resides in an assembly with the same name.

Simple, but interesting , huh?