Saturday, August 30, 2008
Silverlight on the Desktop
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?
(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:
- Improved Templating model using the Visual State Manager
- New Tab Control
- DataGrid improvements: Autosize, Reorder, Sort, Performance increase
- TextBox: Text wrapping and multiline selection highlighting in textbox.
- Includes the Visual Tree Helper.
- DataBinding: Binding to attached properties is now supported.
- 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 :).
- DeepZoom: XML based file format
- 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).
- 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”.
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
- 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
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?
Wednesday, April 30, 2008
Testing Silverlight Streaming
If you are reading this blog, you certainly know about the Silverlight streaming service. But did you know that you can stream not just media but an entire Silverlight application? That's right, you can upload your Silverlight application to the Silverlight streaming server, and they take care of the rest. The video and button you see on this page are from a simple test app I uploaded to the server. All you need to do is just paste the html code they provide you after you upload your application.
Want to try this out? This post by Tim Sneath is a great place to start!
Friday, April 25, 2008
Using the WebClient to Download Content On-Demand
Source (Beta 2): The Beta 2 source is available here.In this post I'm going to describe how you can use the WebClient class in Silverlight 2 to download content on-demand.
Firstly why would you want to download something on-demand? Say your application is displaying images, video, lots of XAML etc. Now if you allow the appplication to download all of this initially to the client, the user may get impatient because of the long waiting period. You may lose your user even before he has had a first look at your application.
So it makes sense to only include in the initial download, what is absolutely essential for the application to load. Once the application is loaded, you can start downloading other parts of the application behind the scenes. This is where downloading content on-demand comes into picture.
We'll look at three scenarios.
- Downloading a loose image file on-demand.
- Downloading a zip file on demand and retrieving an image file.
- Downloading an assembly on demand, and thereafter using classes within the assembly
The source for the sample application I created to illustrate this is available here.
Downloading a loose image file on-demand
In Silverlight 1.0, the class used for downloading content on-demand was the Downloader class. In Silverlight 2, we use the WebClient class.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted1);
wc.OpenReadAsync(new Uri("bird.jpg", UriKind.Relative));
}
void wc_OpenReadCompleted1(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null);
BitmapImage imgsrc = new BitmapImage();
imgsrc.SetSource(sri.Stream);
ImgToFill.Source = imgsrc;
}
}
This code is quite self-explanatory. You need to call the OpenReadAsync method ofthe WebClient, and then retrieve the image in the OpenReadCompleted event.
Downloading a package (zip file) on-demand and extracting an image file
In this case the code is similar.
To download a package
- Copy zip next to xap file
- Download as stream using WebClient
- Use StreamResourceInfo class to retrieve the desired part from the package stream
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted2);
wc.OpenReadAsync(new Uri("mediaassets.zip", UriKind.Relative), "Garden.jpg");
}
void wc_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null);
String sURI = e.UserState as String;
StreamResourceInfo sri2 = Application.GetResourceStream(sri, new Uri(sURI, UriKind.Relative));
BitmapImage imgsrc = new BitmapImage();
imgsrc.SetSource(sri2.Stream);
ImgToFill.Source = imgsrc;
}
}
Notice that after downloading the zip package, we did not have to unzip it explicitly. The StreamResourceInfo class took care of that.Downloading an assembly on-demand and extracting an image file
What do we mean by downloading an assembly on-demand? Say you a large assembly that contains content like user controls, XAML etc. that you want to use in the application, but not in the first page that the application loads.
In this case you you can download the assembly on-demand instead of loading it at the beginning when the application is loaded by the client.
One question I had when I read of this concept was, wouldn't you have to reference the assembly in the application at design time itself to compile code that makes of its classes? And if you reference the assembly, won't it become part of the application's initial download to the client?
Yes, it is true that you need to reference the assembly, but you set the private attribute of the csproj file for that assembly to false. In this case, the assembly will not become part of the xap package, but you can still call classes of this assembly in the code base.
<ProjectReference Include="..\LargeSizeAssembly\LargeSizeAssembly.csproj">
<Project>{51238BB3-5EC5-4B01-BABD-779DBF69F623}Project>
<Name>LargeSizeAssemblyName>
<Private>FalsePrivate>
ProjectReference>
Summarizing the steps, this is what you need to do:- Add reference to the assembly
- In the csproj file:
set the 'Private' tag for that assembly to False - Copy assembly next to xap file
- Download as stream using WebClient
- Use AssemblyPart class to load assembly to current AppDomain
- Now start using classes in the assembly
private void Button_Click_3(object sender, RoutedEventArgs e)
{
// Download assembly "on-demand"
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted3);
wc.OpenReadAsync(new Uri("LargeSizeAssembly.dll", UriKind.Relative));
}
void wc_OpenReadCompleted3(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
//Uncommenting the line below will throw an exception because the assembly
//in which Page2 resides has not been loaded into current AppDomain as yet
//Page2 page2 = new Page2();
// Convert the downloaded stream into an assembly that is
// loaded into current AppDomain
AssemblyPart assemblyPart = new AssemblyPart();
assemblyPart.Load(e.Result);
//now access classes in the downloaded assembly
DisplayPageFromLibraryAssembly();
}
}
private void DisplayPageFromLibraryAssembly()
{
// Instantiate type from assembly
Page2 page2 = new Page2();
// display it
LayoutRoot.Children.Add(page2);
}
You can see how powerful this option of downloading on-demand is. The source for the sample application I created to illustrate this is available here.
