Mix:UK 07: Building Silverlight applications using .net Part 2

I wonder if I made the right decision to only blog the second part, we shall know very soon.


Availability: The control toolkit for silverlight, will be full source soon.

Is it possible to rename assemblies to help with SharePoint? By the next refresh, instead of building the assemblies in separate files, everything will be zipped in a common package: Secure request and dynamically compressed.

Is there a date for CE support? There's a CE version of Silverlight that was shown at the Las Vegas Mix conference. The issue is the distribution aspects on phones. We're trying to sort out a few of these issues.

For those that missed Part 1 [EDIT: I'm the only one in the room it seems], there will be a link to the slides at the end of the session.

As it happens, Part 1 was about Shapes, Controls, UI, with no code. Part two we'll talk about events and write some code, how you can create your own controls, and tak about some of the non UI aspects like networking.

You need to name your controls, which then lets you address them programatically. That's what you'll use to bind events.

The Page_Loaded event is called when the app has been downloaded, which means everything referenced inside the XAML file, including images and videos, the event won't be fired until they've all been downloaded.

What you would want to do is to load all your assets as a background task instead of waiting for everything has been loaded.

Everything youve downloaded goes through the browser cache. You don't have to download assemblies or videos twice, the browser cache handles that.

Question: Can you download byte level access to what gets downloaded? Yes we'll see that later.

You can go and bind event on any UIElement, like MouseMove, MouseEnter, tc. Al shapes and controls derive from UIElement.

You can wire your event inside your XAML file, passing it the method name that will get executed, which is similar to the way it's done in asp.net. Instead of having OnMouseEnter uses MouseEnter and doesn't have the OnXx syntax.

You can also use the Handles keyword from code in VB, or in CSharp in the Page_Loaded event handler.

Question: Can you link to external handlers? Absolutely, not declaratively, but procedurally.

Demonstration: It's a blank page, and wherever you click, it shows a text saying "Clicked". It shows code getting the point where the mouse clicked, create an element, and set the Canvas.Top and Canvas.Left attached property to the newly created element.

Another demo changes the color of a sphere whenever clicked on. By having an Ellipse in the XAML, and in code behind, the Brush, Stroke and Fill properties are being modified programmatically.

It would be painful to add shapes every time you add a button, so let's look at example 3. Three buttons. They're wired to handlers.

Dependency properties will be supported in 1.1, but not sure if property triggers will be supported.

We want people to use Events for designers to stylize the buttons without having developers writting all the wiring code.

To build your own controls, you can do that. You derive from the Control base class. You have full encapsulation, use any shape, controls, brushes, transforms, etc, and expose your properties methods and events you want. They get hooked up to XAML.

As a control developer you can take advantage of the InitializeFromXaml method that lets you get a XAML tree from a string.

To be XAMLable, you have to have a parameter-less constructor, have public properties and public events.

Demo of creating a custom control

You can crate a Silverlight class library. Once it's created, you can add a Silverlight User Control. The XAML from the resource is serialized as a resource, and the partial class loads it from the resource and uses InitializeFromXaml to load it.

There are still a lot of feature missing: TextBox and Input controls are coming on. Layout managers are coming in the final release too. Databinding and DataTemplates, and styling support. All these features will show up in 1.1.

For business apps they are essentials, so it would be a bit hard to develop Silverlight business applications, so you may want to wait.

Is there a licensing model for third party licensing model: There will be a DRM system within Silverlight so you can implement your own.

Is there a way to share common controls so they don't get downloaded all the time? There will be a mechanism for trusted web sites for clients to download only once. There is a versionning policy so if there's a specific version, you bind to that, and we bind versionned, so theres no conflcit there.

One of the core things we try to do is to integrate with existing document to use .net or javascript and HTML in the same application. If you build a .net application, you can import System.Windows.Browser which lets you reach outside and get the HTML DOM, works on all browsers. There's an HtmlPage object so you can navigate to other pages or get all the documents. You can use the GetElementByID to return a reference an object in the page from your code. You can use an AttachEvent to attach a DOM event and give a reference to your .net method, in any language, CSharp, VB, Python, etc. The code for AttachEvent works across all browsers because it's the .net code, it is not related to the javascript way of attaching events.

Question If you want have a client side event handler, what happens? AttachEvent respect the existing attachments.

You can reach out from Silverlight in HTML. You can then do the reverse by exposign public methods in the silverlight controls that you want your javascript in the page. You can mark it with a Scriptable attribute, and register the Scriptable object. Then you can call the FootballData object you registered and call the method marked with the Scriptable.

Question: Would you get a Javascript error before the Silverlight control is loaded? Yes you will, you can check a method to see if the silverlight control is loaded.

You can use Permalinks, integrate forward / backward navigation, etc. We support both simple and complex types across (complex type support not ready yet).

Question: Can you render HTML within Silverlight control? Absolutely. Someone build an HtmlTextBlock for Silverlight, to render HTML within Silverlight (needs to be well formatted) and you can compose HTML and Silverlight controls in the same space.

Demo: HTML interaction.

The title is a silverlight control, the textbox and button are standard html. Enter text and click the button, the sliverlight control update its text by hooking the click event of the html element and updating the TextBlock element within it.

Question: What if you want to gain access to the ClientID? You can for example embed the value as a hidden field or a javascript value.

Demo: Pure HTML page

All the UI is done in HTML. Enter text, click the button, the text appears. What is being used is to hook the button click DOM event, and call silverlight from it. From within silverlight, the .net code creates an html element and adds it to the DOM page. You could then leverage network calls from Silverlight to update data in your html page.

You can also use OpenFileDialog. Often in HTML pages people ask how to add several files in one go. For mutliple files you need several input elements, so you have to do them one at a time. With the OpenFile support in HTML you can't have access to the file from the client. You have to upload to the server, then the client goes back to the server through AJAX or other for the client to consume. The OpenFileDialog makes things easier. You can select single or multiple files, provide filters, dialog, and get what the user sleected (OK, Cancel. etc)

You cannot have access to the original file path or the default path. The user has to explicitly find the phone.

Question: Can it be persisted locally after loaded? We don't give you the path to the file. Can we see the file size: Yes, as well as the file content type.

Let's create a text file. Open a sample, open a file dialog filtered for text files, select the document, the client reads the file and puts it into the silverlight control, the server didn't get hit. You could've done selection of multiple files too.

The code has a OpenFileDialog, with a DialogResult enumeration (very similar to WPF), then reading the content.

What about the network? Same as the .net framework. There's HttpWebRequest (called BrowserWebRequest), which lets you get byte streams in the alpha.

Question: Using local URI? Will throw a security exception. The browser is responsible for cross domain calls. Is there Asynchronous? Yes absolutely, there's a demo.

There's a button, we select an image and upload it to the server. Picture of Scott's sister before the ceremony.

You can also use the web services stack. Instead of using raw HTTP you can create a proxy for JSON (supported in the alpha) and WCF & SOAP (coming soon).

Question: Cross domain? Not supported now, but policy file on the server to give rights supported for release. You can now call any service and integrate it, which opens new opportunities. Will the WCF support will be Duplex? Don't know. Send me an email I'll find out.

You can do an async callback when downloading, prevents the client freezing.

Demo: Two buttons, one call GetMessage method synchronously, the other one asynchronously, which will call another method when downloading is done, freeing your UI thread. Synchronously, the button is frozen while downloading, whereas asynch everything stays responsive.

Question: Is there a need to marshal to the correct thread? For async no because we do it for you. If you use the background thread, you don't marshal but need to let the background thread know. Is it in there? No we pulled it out because it didn't work consistently on all browsers, one of them didn't work so we pulled it out. We won't ship this new thread model when we fix it. Do services call still use the borwsing stack? For Http traffic we still use that so we integrate with the cache. For browsers you can increase the number of connections to a server. There's also sockets support coming. System.Net will work in the sandbox in the future, even though it doesn't work for WPF in xbap.

For anyone building on Amazon S3 servers you'll be able to upload files there. It's great so you don't have to have a server farm to upload your data. You can do most work on the client, a few web servers on the backend, and pay as you go.

Once you download data, you can use the IE / Safari / Firefox cache. But you can also use Isolated Storage as a giant cookie. It's a feature in .net today [EDIT: been since .net 1.0!]. You can read and write from it. There's a 1MB limit for silverlight right now, but we may give the possibility for the user to change it.

Question: Can you have access to other isolated storage from other applications? For the alpha you get a separate store. The plan is for it to be shared per site. Can users turn on and off? The user can clear the cache which clears the IsolatedStorage. We should give the users a way to not allow IsolatedStorage. That's what consumers want.

Silverlight provides LINQ support, but you cannot use LINQ to SQL. This is not using LINQ but the SQL technology, LINQ is a general purpose query language. We'll provide LINQ to XML in the final release, so you can search and sort your xml data you download. We provide LINQ to objects right now. Because LINQ lets you provide your own dataproviders, you'll see LINQ to Flickr, LINQ to S3, which will get translated, so you don't have to know the actual web services to access these services.

Question: Is there a schedule for a refresh? There's no public date for that at the moment. Next milestone will have a lot of low level plumbing to get the layout system and core text input, but there won't be a lot of surface features appearing. It will be later this year but we don't know when yet. We're thinking of not doing a refresh on this infrastructure milestone. Is there a way to have a transform over time (animation support).

Is there support for animation? Demo of animating a transform over time in Blend, which works for Silverlight. You can change the transform type you want, you can loop it, reverse, etc.

The other nice thing from an architecture perspective is that with the increase in multiple core, it's hard from the UI. Silverlight ahs built-in in multicore support, everything gets partitionned on different cores automatically. All the graphics and animations get calculated on separate threads. That means better performance.

Is there a draw API? No we have a retained mode graphics system, so we draw objects on the screen. On multicore and having retained mode, we know how to redraw without you having to write sync, and we know how to draw it efficiently without having to ask you. We can choose to go hardware accelerated without breaking your code. BitmapEffects are not in 1.1, we've not decided to include them or not.

Is there a notion of frame rate? There's a notion of a frame rate, there's an api for that. But because of keyframes this is probably not reliable.

Which platforms are you supporting? Win200, XP, 2003, Vista, Tiger and Leopard, potentially Panther for Microsoft, Novell does Linux called moonlight on redhat, freebsd, solaris, and mobile devices will be announced. Safari and firefox on the max, Firefox and IE, with Opera planned on windows.

Technorati Tags: