3 Small Silverlight tricks you might need some day

This little article describes 3 easy tricks that might come in handy from time to time, namely:

How to create application-wide variables without fancy tricks

Allow a user to install you application as a stand-alone OOB application

Detect whether your application is in browser or “Out-of-browser”

Application-wide variable

Sometimes you are writing a small Silverlight application in which you have a variable that should be usable throughout the whole application. This small trick is a very quick-and-dirty one that should only be used while rapid prototyping as it it’s not very OO/MVVM-friendly.

First you need to create the application-wide variable (and initialize if need) inside the app.xaml.cs file. For example:

publicpartialclassApp : Application{ publicint bigVar;

From now on you can reach any App-variable through the Application.Current object. So if you need to access bigVar from some page in you application (e.g. MainPage) you simply type:

int local = (Application.Current asApp).bigVar;

Show an OOB install button

A very nice feature of SL 4 is the ability to be able to install your application so that it works as a stand-alone application on your host pc. This feature, also called Out-of-Browser (or OOB), can easily be activated through the project-properties:

By enabling this setting the user can right-click anywhere on your SL-application and choose to install the app. As an added benefit, developers can use this feature to allow their application to have elevated permission and thus do more fun stuff.

Alas, many a time it happen that a user doesn’t know the application being used can be installed (how many time does a non-techy richtclick on a website? Not a lot I reckon.). For these people it can be a nice application feature to show some big fancy “Install me” button somewhere on the form. E.g.:

Detect if OOB is running

When running OOB we can do certain things extra thanks to the elevated permissions. Of course, it isn’t nice to our webbrowser-users to show functionality they can’ t use. Using App.Current.InstallState and App.Current.IsRunningOutOfBrowser we can hide or show certain functionality to our users, as follow: