OOB

Window Closing Event

Intro

When running Silverlight 4 out of the browser it could become handy to know that the application is being closed. In case of my open source demo application SilverAmp (http://SilverAmp.CodePlex.com) I wanted to notify the user the application is being closed when music is still playing.

Catching the event

In Silverlight 4 a new event has been added to the System.Window class. This event is raised when the application is being closed. Closed in any way: by the user hitting F4; by the user clicking on the right-top cross; by a calling close on the application from within code or even when windows is shut down.

The great thing about handling this event is that you can cancel it. In the example below the closing event is handled and the user is asked if he wants to exit the application. The application uses a MediaPlayer to play media which is named _internalPlayer. If the currentState of the player is playing than a message box is shown. If the user chooses not to quit, the closing is canceled by setting the Cancel property of the ClosingEventArgs to true.

public MainPage()
{
    InitializeComponent();
    Application.Current.MainWindow.Closing +=
                         MainWindow_Closing;
}
void MainWindow_Closing(object sender, 
                  System.ComponentModel.ClosingEventArgs e)
{
    if (_internalPlayer.CurrentState 
                        == MediaElementState.Playing)
    {
        if (MessageBox.Show(
                "Music is playing, exit application?",
                "Exit",
                MessageBoxButton.OKCancel) == 
                        MessageBoxResult.Cancel)
        {
            e.Cancel = true;
        }
    }
}

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: ,

Elevated Trust

Intro

This is the second article in a series of small articles about what is new in Silverlight 4 and Expression Blend 4. The series is build around a open source demo application SilverAmp which is available on http://SilverAmp.CodePlex.com.

 

Setting elevated trust

A number of new features in Silverlight 4 require more permission, to gain access to the local file system for example. The elevated trust feature is only in available when running outside the browser.

To enable elevated trust right click the Silverlight project and go to the project properties. Check the “Enable running application out of the browser” feature to get access to the Out-of-Browser settings.

clip_image002

On the Out-of-Browser settings panel check the “Require elevated trust when running outside the browser” feature.

clip_image004

To check if the application is running with elevated trust you can use the following property:

Application.Current.HasElevatedPermissions

If this property is true the application is running outside the browser and is in full trust mode.

 

Install Application

When a user wants install the application from the website on his local machine he right clicks the webpage and selects install. When the application is build without elevated trust the normal “install application” window is shown.

image

To make sure the user notices there is a risk when installing the application, instead of the normal popup a security warning is shown when an elevated trust application is installed.

image

So get this security warning a little less scary you’ll have to use a certificate to sign your application. I’ll explain all about signing your applications in a later article.

 

Technorati Tags:

Twitter