Silverlight

Custom Window Chrome

Intro

Today I would like to explain something about customizing the window chrome in Silverlight 4 OOB. For an example of customizing the window have a look at my open source demo application SilverAmp which is available on http://SilverAmp.CodePlex.com.

Choosing style

Running the application without the default window chrome requires running out-of-browser with elevated trust. How to set elevated trust is explained in this earlier post. To set the window style, go to the Out-Of-Browser settings on the Application tab in the Project Settings. At the bottom there’s a dropdown list where you can select your preferred style.

image

  • Default shows the window as you’re used to, with the normal window style.
  • No Border shows a borderless window
  • Borderless Round Corners shows a borderless window too, but with rounded corners.

In the case of SilverAmp I choose to No Border.

Default Windowing Events

In both borderless styles you’ll have to handle the default windowing events yourself. Maximizing, minimizing, moving and resizing are simple calls into Application.Current.MainWindow.

For example:

private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
	if (Application.Current.IsRunningOutOfBrowser)
    {
        Application.Current.MainWindow.DragMove();
    }
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    if (Application.Current.IsRunningOutOfBrowser)
    {
        Application.Current.MainWindow.Close();
    }
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
    if (Application.Current.IsRunningOutOfBrowser)
    {
        Application.Current.MainWindow.WindowState = 
                 WindowState.Minimized;
    }
}

The first event handler handles the MouseLeftButtonDown event on a rectangle representing the title bar of the application.

On this same title bar there are two buttons: Close and Minimize. Both buttons got an Click event handler.

All event handlers perform a check to see if the application is running out-of-browser. Calling these methods inside the browser will cause an exception.

Technorati Tags: Silverlight,OOB

Leave a Reply

Seo wordpress plugin by www.seowizard.org.
%d bloggers like this: