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:

public partial class App : Application
{     public int 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 as App).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.:

<Button x:Name=”installApp” Click=”installApp_Click”>Install me </Button>

The next step is than to use the extremely helpful Install()-method provide by the App.class. Easy as leftclicking a button!


private  void installApp_Click(object sender, RoutedEventArgs e)
{
  App.Current.InstallStateChanged += (Current_InstallStateChanged);
  App.Current.Install();
}

void Current_InstallStateChanged(object sender, System.EventArgs e)
{

if (App.Current.InstallState == InstallState.Installed)
{
installApp.IsEnabled = false;
installApp.Visibility = Visibility.Collapsed;
}

}

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:

if (App.Current.InstallState == InstallState.Installed && App.Current.IsRunningOutOfBrowser)
{

//At specific OOB features here

About these ads

About timdams
C#, .NET, Microsoft, security, .... Read more on : http://timdams.com/

One Response to 3 Small Silverlight tricks you might need some day

  1. Jan says:

    Tim,

    Although a nice trick, adding global variables is not a good programming methodology. That’s probably not the point here, but I though I should mention it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 330 other followers

%d bloggers like this: