Archive | April, 2011

Silverlight 5 – Custom Markup Extensions

20 Apr

A Markup Extension is a class that decides at runtime how to set a property’s value. Markup extensions allow code to be run at XAML parse time for both properties and event handlers.

In Silverlight 4 we got some different Markup Extensions:

  • Binding
    Text=”{Binding Path=FirstName, Mode=TwoWay}”
  • StaticResource
    Foreground=”{StaticResource BlueSolidColor}”
  • TemplateBinding
    VerticalAlignment=”{TemplateBinding VerticalContentAlignment}”
  • NullExtension
    Background={x:Null}
  • RelativeSource
    DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"

With Silverlight 5 we get Custom Markup Extensions allowing us to write more specific and complex Markup Extensions.

Jeff Prosise’s have written a good sample here and Jeremy Likness have written about getting creating a custom markup extension that uses MEF to satisfy imports on object instances declared in XAML here.

by xamlgeek
@thomasmartinsen

Silverlight 5 – Elevated Trust In-Browser

20 Apr

In Silverlight 5 it is possible to enable applications to run with elevated permissions in-browser.

The code snippet below will open Notepad.exe using the AutomationFactory in Silverlight. Using the AutomationFactory requires elevated permissions.

clip_image001

To run the snippet in Silverlight 4 the application must run with elevated permissions and in out-of-browser. In Silverlight 5 it is possible to run the snippet in-browser and with elevated permissions.

There are some requirements that need to be fulfilled to be able to run with elevated permissions in-browser. The XAP file needs to be signed with a certificate present in the Trusted Publishers Certificate store. Furthermore it is necessary to add a registry setting:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Silverlight\

Add the following DWORD key "AllowElevatedTrustAppsInBrowser" with 0x00000001.

In order to make development a bit easier running using localhost (or 127.0.0.1) won’t require signing the XAP file or adding the registry setting.

You can download the demo here.

by xamlgeek
@thomasmartinsen

Slides from my WP7 talk at MOW2011

15 Apr

Today I did two talks about Windows Phone 7 at MOW2011 in Billund, Denmark.

First talk was a general talk about Windows Phone 7 and how you begin Windows Phone 7 app development.

Slides | Demo

Second talk was a bit more technical and was about styling, notifications and localization in Windows Phone 7.

Slides | Demo

Hope you enjoyed it :)

by xamlgeek
@thomasmartinsen

Silverlight 5 – ClickCount

13 Apr

Silverlight 5 enables support for multi-click input on left and right mouse button making it easier to implement features like double click. ClickCount is available on MouseButtonEventArgs.

clip_image001

by xamlgeek
@thomasmartinsen

Silverlight 5 – Multiple Windows Support

13 Apr

Silverlight 5 supports the ability to open multiple top level windows. It is required that the application run with elevated permissions.

clip_image001

Closing the main application window will cause all windows to close.

I have added a small demo here.

by xamlgeek
@thomasmartinsen

Silverlight 5 – Implicit DataTemplates

13 Apr

With Silverlight 4 came implicit styles. With Silverlight 5 comes implicit DataTemplates. Implicit DataTemplates allow DataTemplates for ContentPresenters to be selected based upon the type of its content.

In this sample I have created a Person entity containing a firstname and lastname property. For sample purpose only I have created a DependencyProperty of type Person that I can bind to in the XAML.

clip_image001

In the XAML I have added a ContentControl and bound the Content property to the Person DependencyProperty.

clip_image002

Using the new implicit DataTemplate feature I have created a DataTemplate based upon a type as a resource and the DataTemplate will be applied to the ContentControl.

clip_image003

Implicit DataTemplates can be added at Application level or locally in controls as done in the sample above.

You can download the sample here.

by xamlgeek
@thomasmartinsen

Silverlight 5 beta

13 Apr

Silverlight 5 Build 5.0.60401

Today at MIX11 keynote Scott Gu announced the beta version of Silverlight 5.

The release contains some really interesting features. Some of them got focus on line-of-business applications, some on media applications and some are more general like performance optimization and debugging bindings (yeah!).

Among the released features are:

The Silverlight 5 Beta is a major step toward the final release and includes many of the significant features already announced. The Beta download link and full list of features is available at http://www.silverlight.net. Visual Studio and Expression Blend tooling support for the Silverlight 5 Beta is also available.

Tim Heuer about the new release.

by xamlgeek
@thomasmartinsen

Share code between client and server

10 Apr

One of the challenges of writing managed components for Silverlight is the fact that it is not possible to use a normal .NET assembly in Silverlight projects. One way to get around this issue is to use linked and partial classes in Silverlight projects.

How to use linked classes in Silverlight

Let’s say I have a solution with 3 projects: a Silverlight client (client), a web application (web) and a .NET class library (lib). The lib contains entities and business logic that I wish to share between the client and the web.

It would be great to reference the lib from the client but that is not an option in Silverlight. Of course I can copy the content of the lib to the client, but that is really not an option either; nobody wants to maintain to sets of the same business logic.

Instead I can link the files from the lib to the client. In this sample I have a Person entity in the lib project that I wish to share with the client. In the client I choose to add an existing item and find the class from the lib project I wish to add. I click on the arrow next to the add button and select "Add as link".

clip_image001

clip_image002

This will add the Person entity class to my client project and it’s nothing more then a link to the class from the lib project. The link will make sure that the class is compiled into both projects at compile time.

The downside of this solution is that even though I can share the classes, it’s not possible to put logic into the classes that is server side or client side only. One way to get around that is to use partial classes.

Partial classes on the client and the server

In the lib project I can create a server side partial class of our business logic and I can do the same on the client. In this sample I have create a server side only version of the Person entity classes "Person.server.cs" and a client side only version of the Person entity called "Person.client.cs". The Person entity itself is still linked between the client and the server. Remember that for this to work the namespace must be the same between all partial files! This enables the separation of logic running only server or client side.

clip_image003

This solution enables me to run and share classes like business logic code on the client and the server.

You can download my sample here.

by xamlgeek
@thomasmartinsen

Follow

Get every new post delivered to your Inbox.

Join 1,356 other followers