VB On Silverlight

VB On Silverlight

  • Comments 32

 

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

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.)

Supported…

  • 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)
  • Math utilities: Rnd(), Random()
  • IIF()
  • 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

    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.

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/.

Workarounds

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…

  1. Close Microsoft Expression Blend.
  2. Navigate to \Program Files\Microsoft Expression\Blend 1.1\.
  3. Create a new XML file in that folder and then rename the file to Blend.exe.config.
  4. Paste the following XML snippet into Blend.exe.config.
  5. Save Blend.exe.config and close it. 
  6. Open Microsoft Expression Blend. Select “Visual Basic” as the language and you can create “Silverlight Application” projects.

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>

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.

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

    End Sub

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

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
  • Joe &amp; The VB Team has a tip for building Orcas projects with Expression Blend 2 . This trick allows

  • OK, i found the blog i was referring to in my previous comment:

    http://blogs.msdn.com/xmlteam/archive/2007/04/30/live-from-mix07-silverlight-and-xml.aspx

    this seems to rule out LINQ2XML only for 1.1 alpha.

    infoq has already reported that LINQ2XML was cancelled for silverlight 1.1, so you should probably make that clear.

  • Does Silverlight run on Internet Explorer of a PDA running CE.Net 4.2 or CE.Net 5.0 ?

  • Does Silverlight run on Internet Explorer of a PDA running CE.Net 4.2 or CE.Net 5.0 ?

  • What happens if people type iif instead of if or vice-versa by accident? Also IntelliSense autocomplete might mess up with their typing without them noticing. Should have some way to minimize typos there

  • I was starting to think I had a good grasp on the technologies Microsoft was bringing out with Orcas,

  • Like the previous comment, I am beginning to give up on my efforts to keep up to date with products being churned out by Microsoft. You don't get a full grasp of one product or tool before a new one or version is released. I know there is competition from other platforms but PLEASE slow down.

  • Wow!

    it's amazing to work with the silver light environment ,i am a teacher in csc computer education it's interesting to teach silver light tools

  • Ktoś tu coś napisał po japońsku. Czy może ktoś przetłumaczyć? To na pewno bardzo ważne.

  • Microsoft Silverlight is a cross-browser , cross-platform plug-in for delivering the next generation

  • from server side can I call com object such adodb??

  • I've kept away from Silverlight for a bit since it was a bit too new for me. Now that I've had more time

  • You're right about one thing, there are so many products coming out I can't keep up.  Silverlight 1.1?  I didn't even think 1.0 was really here yet!

    Well I applaud this effort, I think having cross platform VB is awesome!  I have really been thinking that MS was giving up on VB.  Every job posting you see these days is for C#.  Maybe there is a future for VB here.

    Anyway, about the language... I cannot emphasize enough that using IIF in code is BAD practice.  Always has been.  Yeah I know there is the similar construct in C like languages, but I think it is bad there too.  Using good old IF-Then-Else promotes readability, efficiency, and code-correctness.  I wouldn't waste the space on it.  And I won't use it, nor accept any developers turning in code with it.

    viva VB!!!

  • During the weekend I spent some minutes to collect some of the greatest Silverlight examples. Most of

  • Been in Seattle all week at an internal conference called "TechReady" watching the latest and greatest

Page 2 of 3 (32 items) 123