Rants Tagged with “Silverlight 2”
Now that .NET 3.5 SP1 and Visual Studio 2008 SP1 have been Beta'd, I've been spending a lot of time digging deep into the ADO.NET Data Services in that release. Because of this, i've been very quiet. Look for this blog to get very loud in June after TechEd (no, I won't be there).
I will be releasing a new version of my Silverlight Sample Site (www.silverlightdata.com) with ADO.NET Data Services and Forms Authenticaiton support in the next few weeks. Keep your eye here!
For the upcoming San Francisco stop of the Silverlight Tour starting May 28th, there are only five seats left. If you want to spend days with me discovering Silverlight 2, Beta 1, your time is running out.
You can see a full outline of the three day course here:
http://silverlight-tour.com/outline.aspx
I was teaching the Silverlight Tour this week and a student asked me some advice on how to implement uploading status messages using Silverlight. The question stuck in my brain last night and I came up with a quick example but quickly hit the 4MB upload limit of standard ASP.NET Requests. I knew I could create a web-service or web page that could handle chunking the larger pieces but I was concerned about the problem of incomplete transfers and leaving a mess on my web server's disks.
It seemed to me there had to already be a solution that I could adapt for Silverlight. After a quick Twitter plead, Jon Galloway had a great link for me that had already done it in Silverlight (though it might be Silverlight 1.1). Wilco Bauwer had a good example using Silverlight and a HttpHandler to do uploads to ASP.NET. Follow the link above for a solution. I suggest getting the code and reading it. What he is doing is pretty interesting and informative.
JavaFX was announced at JavaOne this week. What is it? Its a RIA platform for creating compelling in-browser applications. Certainly this looks like its a competitor for Flash and Silverlight. Interestingly, instead of being able to write Java (at least from the quick look I had), it looks like they created a new script language (called JavaFX Script) for it. Seems like a step backwards...but that's just me.
What it does have going for it that Silverlight doesn't is interoperability with NetBeans and tools for the Mac. I don't think NetBeans interoperabilty is important, but tools for the Mac is IMHO. What do you think?
With the asynchronicity question still dogging Silverlight 2, I thought I'd mention an oft forgotten little class in Silverlight (and in WPF) called the Dispatcher. Much of the confusion with asynchronous programming seems to stem from the fact that developers over complicate the problem. Think that they need to handle the cross thread calls themselves. They tend to create two way communications for this or overuse the BackgroundWorker's ReportProgress functionality.
The key to simplifying calling the UI thread is the Dispatcher class. This class supports a static (or shared) interface for executing code on the UI thread. For example, you can call Dispatcher.BeginInvoke to invoke some arbitrary code on the UI thread:
// With Simple Lambda
Dispatcher.BeginInvoke(() => DoSomething());
// Also With Lambda
Dispatcher.BeginInvoke(() =>
{
DoSomething();
}
);
// or with Anonymous Delegate
Dispatcher.BeginInvoke(delegate()
{
DoSomething();
} );
This is all is required to execute code on the UI thread. The Dispatcher guarantees that this code is executed on the main UI thread. This is simplified versus the same WPF code. The Windows Presentation Foundation uses a a prioritized message queue so that the Dispatcher really allows developers to not only make calls to the UI thread, but do so with some priority attached. (See my MSDN article on the Dispatcher in WPF for a more detailed explanation). In Silverlight 2, the model is simplified (for better or worse). When calling the UI thread, you simply need to specify the code to call on the UI thread...the priority is gone.
Unfortunately there is no way currently to reliably test for the UI thread (again unlike in WPF where the DispatcherObject was part of the object hierarchy so you could call CheckAccess to see if you were on the UI thread). Because of this Silverlight 2 has a fundamental problem. It is unclear when we are on the UI thread. This is especially problematic for the event driven architecture that Silverlight 2 employs. This is exemplified by the fact that during some events (WebClient calls are especially prone to this behavior) seem to fail silently when you try and update the UI. This is unlike the BackgroundWorker who throws an UnauthorizedAccessException (with the message of Invalid Cross Thread Access). So at times I find myself throwing in Dispatcher.BeginInvoke calls if the UI is failing to update in events (both control events and other events). This isn't a great solution but it does solve the issue much of the time.
I've attached a simple Silverlight 2 sample that shows this technique. Let me know if you have any questions about this sample.
I hate to just link other blogs, but this example from Brad Abrams is a biggie. In this entry he shows how to consume the ASP.NET Authentication, Profile and Role Providers from Silverlight 2 code. It is an impressive example and is well worth the time to dig into the code.
The Expression team has released a new Beta version of the Deep Zoom Composer with a plethora of changes. These include:
- Better Exporting
- Improved Design Experience
- Better Collection Exports
- Help Actually works.
If you're looking at Deep Zoom, this is a must have.
In an interesting development that I can only assume means that Silverlight is having an impact on Adobe, they've decided to open and standardize their FLV and SWF formats so that anyone can build them. Not only are they opening the formats but also removing licensing fees for mobile platforms. Nice!
This is good news for Flash/Flex/AiR developers out there. In my opinion this also bodes well for Silverlight as it means that Adobe is taking the Microsoft platform as a serious competitor.
What do you think?
Originally I had planned to start my screencast this week with a how-to on creating a sort of fake dialog window in Silverlight 2. Unfortunately I got the flu so I've decided to put off the screencast for another week but share with you how to create the fake dialog, but in full fidelity text and code examples. If you cross your eyes, it looks like its even in 3D.
The cornerstone of creating a fake dialog in Silverlight 2 is the Popup control. The Popup control is a special container that when shown always shows up over other controls without taking up any space. For example, here is a simple Silverlight 2 app with a Rectangle and a Button. In addition, we've added a Popup with a simple Grid inside it:
<UserControl x:Class="PopupFun.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Width="200" Height="200" VerticalAlignment="Top">
<Rectangle Fill="Blue" Width="100" Height="100" />
<Button x:Name="showButton" Content="Click Me" />
</StackPanel>
<Popup x:Name="fakeDialog">
<Grid x:Name="theBack" Background="#80000000">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border BorderBrush="Black"
BorderThickness="2"
Background="WhiteSmoke"
CornerRadius="15"
Grid.Column="1"
Grid.Row="1">
<StackPanel Margin="8" Width="200" >
<TextBlock TextWrapping="Wrap">
This is just some sample text. It doesn't
matter what it says as long as it is long
enough to actually wrap
</TextBlock>
<Button x:Name="closeButton"
Content="Close"
VerticalAlignment="Bottom" />
</StackPanel>
</Border>
</Grid>
</Popup>
</Grid>
</UserControl>
When we show the page initially, the Popup is hidden (because it's IsOpen property is false by default):
But when we change the IsOpen property (in the Click eevent of the Button),. the Popup is shown on top of the existing content:
What is particularly interesting about the Popup control is that it takes no space in whatever container it resides in. The Popup control is meant to shown over the existing content. The problem is that you will want to take over the entire screen with the fake dialog. In this case the size of the Popup won't cover the entire screen by default. To fix this we can register for the Application's Resize event to change the size of the Grid (named "theBack") inside our Popup:
App.Current.Host.Content.Resized += (s, e) =>
{
theBack.Width = App.Current.Host.Content.ActualWidth;
theBack.Height = App.Current.Host.Content.ActualHeight;
};
This will resize the Grid whether the Popup is shown or now. Now that we have the resizing code in place, the dialog takes over for the entire screen:
There are a number of things you can do with a Popup control, but this should give you a quick primer on how the Popup control really works. The source project can be downloaded at the link above.
With the success of our first two Silverlight 2 Workshops, the Silverlight Tour is expanding to three new cities and more stops. The new cities are:
- New York, NY
- Denver, CO
- San Diego, CA
We've also expanded our schedule out to the end of the year. We'll be returning to Seattle and Dallas. Don't miss out on three days of Silverlight 2 training in a city near you.