Tag Archives: Silverlight 4

Update XAP file

27 Aug

Installing and running a Silverlight application out-of-browser makes it possible to create a better experience to the user. Sometimes it is necessary to update the installed application and with the build-in functionality in Silverlight it is a pretty straight forward operation.

Depending on your application the update should be done early on in the application. Normally I do it as part of the Application Startup event before setting the RootVisual.

First we need to make sure the update is only done if the user is actually running the application out-of-browser. Application.Current.IsRunningOutOfBrowser will be true if the user has installed the application and running it out-of-browser.

2011-08-27_094955

On the Application.Current object we can call the CheckAndDownloadUpdateAsync method. This will check if an update is available and download it. The method is an asynchronous method and we need to subscribe on its complete event to complete the update.

If an update is available (and have been downloaded) the UpdateAvailable property of event arguments for the complete event will be true. The update (the new XAP file) have been downloaded but not installed as it is not possible to remove the old XAP file since the user is actually using it to run the application. Therefore the user need to restart the application to install the update.

2011-08-27_100104

In some applications it is ok not to require the user to restart the application but in most scenarios it is the preferred solution – think about service updates and so on.

At the moment it is not possible to separate the check and download. It is not possible to check if an update is available and the ask the user if she wish to actually download the update. If the CheckAndDownloadUpdateAsync method is called both check and download will be invoked.

In most debug scenarios this update check will be annoying since the XAP file will be updated everytime you build your project. To overcome this I normally adds an if-not-debug (#if !DEBUG) around the update check.

Source code is available here.

by xamlgeek

Silverlight 4 minimum requirements

22 Sep

If you’re looking for the minimum requirements for Silverlight 4, you will probably browse to http://silverlight.net or to http://microsoft.com/silverlight. However if you go to http://www.microsoft.com/silverlight/faq/#sys-req you will get disappointed – the minimum requirements listed is for Silverlight 3.

Windows: Intel® Pentium® III 450MHz or faster processor (or equivalent)
Mac OS: Intel Core™ Duo 1.83GHz or faster processor

I was told by Microsoft that the minimum requirements remains the same for Silverlight 3 and Silverlight 4. However if you go to the Silverlight install page you can see the minimum requirements for the most current version of Silverlight (4.0.50826.0):

Windows: X86 or x64 1.6-gigahertz (GHz) or higher processor
Macintosh: Intel Core Duo 1.83-gigahertz (GHz) or higher processor

I’m confused… Well, for future requests regarding the minimum requirements I would choose the Silverlight install page.

Edit: Turned out that the information on the FAQ was wrong and will be corrected. Minimum requirements is 1.6 GHz.

by xamlgeek
twitter.com/thomasmartinsen

Issue with Silverlight OOB warning

10 Jun

With Silverlight 3 we got the opportunity to create Silverlight applications that install on the client machine. With Silverlight 4 we can require that the applications get elevated permissions to the client machine. We are able to access local files on the client machine and communicate with hardware and installed applications using COM under the same privileges as the logged in user.

It’s important that the user understands that an installed Silverlight application that runs with elevated permissions should be considered just like any other installed application. It’s no longer a web application that runs in a sandbox!

Scenario 1 – install without signed application

By default, when the user choose to install a Silverlight application that requires elevated permissions she get a security warning that the publisher of the application could not be verified.

security_warning_unverified

“This application does not have a valid digital signature that verifies the publisher. You should only run software from publishers you trust.”

The security warning does not tell the user anything about the harm that this application potentially can course. Instead it has focus on the missing certificate.

Scenario 2 – install signed application

If we try to sign the application with a certificate and install the application again we will see a much more accurate security warning.

security_warning_verified

 “This application can potentially access your personal data and harm your computer. Only install applications from sites you trust.”

The security warning now inform the user about the harm that this application potentially can course. Why isn’t the user warned in the first scenario? I hope (and believe) that this be fixed in a future version.

by xamlgeek 
twitter.com/thomasmartinsen

Use MEF to load ViewModel in Silverlight 4

24 May

It has become increasingly popular to use the Model-View-ViewModel (MVVM) in Silverlight. It’s easy to use the MVVM in Silverlight and it opens up for some extended testing scenarios. With Silverlight 4 we can also use some of the features from MEF (Managed Extensibility Framework). One scenario is to use MEF to load the ViewModel to a View.

How to do it

For this demo I have created a Silverlight project with a view called CustomerView.xaml and a viewmodel called CustomerViewModel.cs. The view contains an ItemsControl with an ItemTemplate that I will use to display a list of Customers and their address. I have bound the ItemsSource to the Customers property from my viewmodel.

image

In the code-behind of the MainPage.xaml file hosting the view, I have created a property called ViewModel. When the ViewModel property is set it will set the DataContext of our view to the provided value. I have added an Import attribute to the ViewModel. The Import attribute declare that this property relies on a service provided by some other component.

image

I have added an Export attribute to the CustomerViewModel class. The Export attribute declares that this class provides a particular kind of service.

image

To connect the components together I need to resolve the Imports and Exports. It is done by a class called CompositionContainer. In most cases you will just call CompositionInitializer.SatisfyImports method to try and provide components that export all the things you need to import.

image

You can write your own CompositionContainer explicitly if you want.

My viewmodel will now be resolved and an instance will be set the DataContext of the view.

The consequenses

This is a very simplified sample and can seem a bit like shooting birds with cannons. In larger scale you will really see the power of using this solution where your views do not need to know about its viewmodel.

This solution will let you use an other viewmodel in designtime supporting the use of using a viewmodel and Expression Blend.

On the other hand this solution will add some complexity to the project. It will be harder to follow the code and it is often not clear from looking at the code what sorts of objects you’re really using or how they were instantiated.

Sample code

Get the sample code here: http://bit.ly/bxZ1Ly

by xamlgeek 
twitter.com/thomasmartinsen

New message in Outlook using Silverlight 4 and AutomationFactory

29 Mar

Silverlight 4 makes it possible to use the AutomationFactory (COM) to communicate with applications installed on the client computer.

To use the AutomationFactory it’s required that the application runs as an out-of-browser application with elevated trust. Furthermore it’s required that the user  can use the COM (the client must run on a Windows computer for COM to work – Microsoft have stated they are looking into getting COM+ support working in Silverlight 4 RTM on a Mac).

image

In the sample above I first of all check that the user is running the application with elevated trust and that the AutomatationFactory is available. I create a new dynamic object as an instance of Outlook and a new mail object. I set the To, Subject and Body on the mail object, saves it and finally sends it.

by xamlgeek

ScriptNotify in Silverlight 4

29 Mar

Silverlight 4 introduces a WebBrowser control which I have written a short post about this control here.

In some scenarios it is necessary to get a response from the WebBrowser control – to do that you can use the ScriptNotify event on the WebBrowser control. The ScriptNotify occurs when the content contained in the WebBrowser control passes a string to the Silverlight plug-in by using JavaScript.

To demonstrate the functionality of ScriptNotify event, I have created a Silverlight 4 project and an ASP.NET webproject that hosts the Silverlight project. I have made the Silverlight project an out-of-browser application – this is necessary to display HTML in the WebBrowser.

On the server-side I have added an HTML-file called HTMLPage1.htm that will be the HTML-file that I displays in the WebBrowser. I have added a button to the page and added a JavaScript function called notify that is invoked when I click the button.

image

In the Silverlight project I have added a WebBrowser control on the MainPage.xaml and I have set the source to our HTML-page on the server.

image

In the code-behind I subscript to the WebBrowser’s ScriptNotify event and implement an EventHandler.

image

To invoke the ScriptNotify event from the HTML-file I need to pass a string to the Silverlight plug-in. The Silverlight hosting environment allows you to call Notify on the Window.external object and pass a string to the plug-in. When you do this, the ScriptNotify event occurs.

image

My ScriptNotify event catches the call from the JavaScript function and from the NotifyEventArgs in the EventHandler I get the string value sent from the JavaScript function.

image

You can download the demo project here.

by xamlgeek

Silverlight 4, Implicit styling and TargetTypes

4 Mar

One of the new features in Silverlight 4 is implicit styling (thank you, Microsoft!). To prevent unwanted surprises of inheritance there some limitations. It’s only possible to set the exact TargetType on a style that you wish to use. Hence it’s not possible to set TargetType to ButtonBase and let all types of buttons (Button, CheckBox, RadioButton…) implicit use the style.

http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx

by xamlgeek