A group blog from members of the VB team
One of the great things about being on the VB team is that we’re generally in the loop early and often with teams developing new technologies and products. It’s always exciting to see ideas take shape, evolve into a product, and eventually hit the market—often after four or five code names, a couple of tech previews, and a few release date/year extensions. Despite my best efforts, the sheer volume of new stuff coming out is a bit overwhelming; and maintaining more than a cursory understanding of the products that are incubating at Microsoft is pretty challenging. But when some partner teams started asking what it would take to write VB.NET code-behind for a browser plug-in that could run cross-browse and cross-platform, my interest was definitely piqued. “Let me get this straight… You want to write a web plug-in VB.NET… that doesn’t require the full .NET framework to be installed… and runs on the Mac… and Firefox … but supports the full VB.NET language? Uh… Okay, let’s chat.”
It has only been a few months and a handful codename changes since we had that discussion (For a humorous history of Silverlight’s codenames, check out Tim Sneath’s blog.) , and despite a number of obstacles, we’re getting there with the Alpha release of Silverlight v 1.1. And while I’m by no means a Silverlight guru, I have been using it for a little while now and I thought I’d shed some light on what’s out there for VB developers who are interested in trying out Silverlight v1.1.
Prior to the announcement of Silverlight 1.1, Silverlight plug-ins were written in XAML and Javascript. While the Javascript model is certainly powerful, using VB is always my preference—it’s a language I know well, and its flexibility suits my programming style. Not to mention that the introduction of VB.NET into the Silverlight world brings with it many of the .NET Framework’s types and APIs I’ve come to rely on.
If you’ve had a chance to download the Silverlight 1.1 runtime, you may have noticed that it’s MUCH smaller than the full .NET Framework at 4.2 MB (and hopefully shrinking!). Clearly, some things have been factored out of the .NET Framework to keep the download size down and installation time short. And although a lot has been removed, the core set of types is included along with some APIs that are particularly useful for writing plug-ins—things like a “lite” version of System.Xml.dll, support for making web service calls, and so on. When deciding what aspects of the Visual Basic language and runtime should be included with Silverlight v1.1, we tried to follow a similar pattern: we wanted to preserve the core VB programming experience—late binding, conversions, Linq, and so on; and we didn’t want to bloat the download size with anything that wasn’t absolutely necessary or didn’t make sense in Silverlight—things like some of the COM helpers (COM is not supported in Silverlight), financial functions, etc. It was a delicate balance to strike between functionality and overall size. The Silverlight 1.1 Alpha is our first crack at it… and it may be refined over the coming months with feedback. Here’s a laundry list of what’s supported/not supported currently. (If you’re interested in digging into the details, you can also view the types in the Object Browser using the tools described below.)
As a sort of test piece, I recently took a look at the Clock Sample and wanted to see if the feature set we settled on enabled me to write VB code as I normally would for a Windows Forms or WPF application. The code basically just sets the minute, second, and hour hands on the clock to the current time (approximately). Here’s what it looks like…
' Anonymous delegates, Handles--isn't it pretty? Public Sub Canvas_Loaded() Handles Me.Loaded InitializeComponent() ' Type inference; DateTime functions Dim currentTime = Now ' Conversions, type inference Dim hourAngle = ((currentTime.Hour / 12) * 360 + currentTime.Minute / 2) + 180 Dim minAngle = ((currentTime.Minute / 60) * 360) + 180 Dim secAngle = ((currentTime.Second / 60) * 360) + 180 ' hourAnimation is a generated variable that makes use of WithEvents and Conversions ' Protected WithEvents hourAnimation As DoubleAnimation ' Me.hourAnimation = CType(Me.FindName("hourAnimation"),DoubleAnimation) hourAnimation.From = hourAngle hourAnimation.To = hourAngle + 360 minuteAnimation.From = minAngle minuteAnimation.To = minAngle + 360 secondAnimation.From = secAngle secondAnimation.To = secAngle + 360 End Sub
' Anonymous delegates, Handles--isn't it pretty?
Public Sub Canvas_Loaded() Handles Me.Loaded
InitializeComponent()
' Type inference; DateTime functions
Dim currentTime = Now
' Conversions, type inference
Dim hourAngle = ((currentTime.Hour / 12) * 360 + currentTime.Minute / 2) + 180
Dim minAngle = ((currentTime.Minute / 60) * 360) + 180
Dim secAngle = ((currentTime.Second / 60) * 360) + 180
' hourAnimation is a generated variable that makes use of WithEvents and Conversions
' Protected WithEvents hourAnimation As DoubleAnimation
' Me.hourAnimation = CType(Me.FindName("hourAnimation"),DoubleAnimation)
hourAnimation.From = hourAngle
hourAnimation.To = hourAngle + 360
minuteAnimation.From = minAngle
minuteAnimation.To = minAngle + 360
secondAnimation.From = secAngle
secondAnimation.To = secAngle + 360
End Sub
All in all, this particular example looks and feels pretty VB to me—which isn’t to say we’ve nailed it by any means… but the example does give some indication as to the type of VB experience we’re striving for with Silverlight. Let us know what you think.
Unless you’re really into notepad , download the tools for Silverlight. The VS project templates alone are worth the bandwidth required, as they’ll get you started in the right direction. In addition, many of the more interesting things you can do with Silverlight (e.g., animations, embedding videos, etc) are pretty tedious to wire-up by hand in XAML; using Expression Blend makes the process *much* easier. All of the components mentioned below are available at: http://silverlight.net/GetStarted/. I’ve just included them here for convenience.
If you run into any problems getting this stuff installed, please post the problem at the forums, http://silverlight.net/forums/.
Okay, almost there… we just need to work around one somewhat confusing bug in Blend you’ll run into when you try to create a VB project:
The problem here is that Expression Blend is configured to build VS 2005/VB 8.0 projects, but only VB 9.0 supports building Silverlight 1.1 applications. To work around the issue, you’ll have to re-configure Blend to use VS Orcas & VB 9.0 (Sigh!). Here’s how…
Blend.exe.config
<?xml version ="1.0"?> <configuration> <startup> <supportedRuntime version="v2.0.50727" safemode="true"/> <requiredRuntime version="v2.0.50727" safemode="true"/> </startup> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Build.Engine" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
<?
<
<startup>
<supportedRuntime version="v2.0.50727" safemode="true"/>
<requiredRuntime version="v2.0.50727" safemode="true"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/>
</dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Engine" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
</assemblyBinding>
</runtime>
</configuration>
Just keep in mind that if you uninstall Orcas and you want to continue using Blend, you’ll need to delete Blend.exe.config.
You’re pretty much good to go at this point. With the installation of the Visual Studio Add-On, two new project types are available: a standalone Silverlight Web Application and Silverlight Class Library.
I generally find the “Silverlight Project” the most useful template to work with. It includes basic XAML and VB code-behind files, Javascript goo required to instantiate the control, and a boilerplate HTML page that hosts the control. The project can be debugged by F5’ing TestPage.html, although Edit-and-Continue is not supported for Silverlight projects.
The programming model for Silverlight 1.1 is very similar to that of ASP.NET and WPF: the UI aspects of the control are declared in the .xaml file, and events are handled in the code-behind (.xaml.vb) file. Similar to desktop WPF, each named UI element declared in XAML has a corresponding variable that can be referenced in code-behind.
For example, the following TextBlock declared in Page.xaml…
<TextBlock x:Name="TextBlock1" Text="1234" />
Can be referenced in Page.xaml.vb as follows…
Private Sub TextBlock1_MouseEnter() Handles TextBlock1.MouseEnter
Me.TextBlock1.Opacity = 1
If you’re interested in checking out what’s available in the .NET Framework for Silverlight, you can poke around in the Add Reference dialog and/or Object Browser. The former is filtered to only show what you can use in Silvelight 1.1.
The Silverlight tools are very much in an Alpha state, so your mileage may vary a bit. In particular, debugging VB applications can be a somewhat bumpy ride… so please let us know on the forums if you run into any issues.
There are already some pretty vibrant community resources on Silverlight popping up. Many of these include sweet looking samples, and the amount of content should continue to grow in the coming months. The main place to keep an eye on is the main Silverlight site and the Silverlight forums.
By all means, feel free to share your impressions of VB on Silverlight. It’s early and we could really use the feedback. Thanks!
Joe & The VB Team
Been in Seattle all week at an internal conference called "TechReady" watching the latest and
"my interest was definitely peaked" should read "my interest was definitely piqued"