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.
The Visual Basic Language on Silverlight
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.)
- Late Binding: One of the most definitive features of the VB language, I find late binding to be especially useful in writing Silverlight code-behind.
- Conversions: Implicit and explicit conversion operators are available—Ctype, CStr, etc.
- Linq: Currently, Linq over objects is supported
- String utilities: Len(), InStr(), Mid(), UCase(), etc
- VB Collection: Most folks either love it or hate it. A subset of generic collections is also supported. (Non-generic collections are considered obsolete for Silverlight.)
- Dictionary(Of Key, Value)
- List(Of T)
- ArrayList (Obsolete)
- BitArray (Obsolete)
- Hashtable (Obsolete)
- Queue (Obsolete)
- SortedList (Obsolete)
- Stack (Obsolete)
- Dictionary(Of Key, Value)
- Math utilities: Rnd(), Random()
- Information utilities: Things like IsNumeric(), IsDate(), UBound(), LBound(), and so on
- Date utilities: Now(), TimeOfDay(), Year(), etc
- Constants: vbCrLf, vbTab, etc. Some of the more obscure constants have been removed, but the core set is included.
- All core VB Language Constructs: Type Inference, anonymous delegates, Handles, etc
Not Currently Supported…
- The My Namespace: We removed this for Alpha release because much of the existing My Namespace doesn’t make a ton of sense in Silverlight.
- XML Literals: The compiler support for XML literals is present, but System.Xml.Linq (which the feature depends on) is not available in Silverlight 1.1.
- COM Support: Silverlight plug-ins are not allowed to call COM components, so we removed the VB utilities related to COM (e.g., the COM Class attribute)
- FileSystem Object (VB6 Compatibility): Direct access to the file system is not permitted, so these APIs were removed. Alternatively, you might check out System.IO.IsolatedStorage if you need to persist data locally
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
‘ 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
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.
Tools for building Silverlight Plug-ins with VB
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.
What to download
- Silverlight 1.1 Runtime: This is the version of Silverlight that enables you to write VB code-behind. It includes a version of the factored .NET Framework, including the VB runtime (Microsoft.VisualBasic.dll).
- ASP.NET Futures Runtime: Among other features, the controls included in this download make it much easier to embed Silverlight controls and rich media in existing ASP.NET web applications
- VS Orcas Beta 1: Note: You need to have the official Orcas Beta 1! Earlier CTPs won’t cut it. Even if you choose not to use Visual Studio to do your development, you’ll need the Beta 1 VB 9.0 compiler, as it includes some changes required to target the slimmed-down .NET Framework.
- VS Add-On for Silverlight: This component provides some project templates and IDE changes for building Silverlight applications in VB (and C#).
- Expression Blend 1.1 CTP: The VS Add-On mentioned above does not include a designer for Silverlight; editing XAML is done in the XML editor. While the XML editor does support IntelliSense, I find it much easier to perform the visual and story board layouts in Expression Blend. (See the note below for the workaround required to use VB & Blend.)
- Silverlight SDK: The most interesting content in the SDK is the reference documentation for the VB runtime. It basically details what functions and types in Microsoft.VisualBasic.dll. The download also includes samples, although I generally prefer to see them online here: http://silverlight.net/quickstarts/managed.aspx.
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…
- Close Microsoft Expression Blend.
- Navigate to \Program Files\Microsoft Expression\Blend 1.1\.
- Create a new XML file in that folder and then rename the file to Blend.exe.config.
- Paste the following XML snippet into Blend.exe.config.
- Save Blend.exe.config and close it.
- Open Microsoft Expression Blend. Select “Visual Basic” as the language and you can create “Silverlight Application” projects.
<?xml version =“1.0“?>
<supportedRuntime version=“v2.0.50727“ safemode=“true“/>
<requiredRuntime version=“v2.0.50727“ safemode=“true“/>
<assemblyIdentity name=“Microsoft.Build.Framework” publicKeyToken=“b03f5f7f11d50a3a“ culture=“neutral“/>
<bindingRedirect oldVersion=“0.0.0.0-220.127.116.11“ newVersion=“18.104.22.168“/>
<assemblyIdentity name=“Microsoft.Build.Engine“ publicKeyToken=“b03f5f7f11d50a3a“ culture=“neutral“/>
<bindingRedirect oldVersion=“0.0.0.0-22.214.171.124“ newVersion=“126.96.36.199“/>
Just keep in mind that if you uninstall Orcas and you want to continue using Blend, you’ll need to delete Blend.exe.config.
A lap around the tools
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.
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.
Additional resources to check out
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