Silverlight 5 Tidbits–Trusted applications Silverlight 5 Tidbits–Markup Extensions

Silverlight 5 Tidbits–Multiple Windows

Published on Saturday, April 23, 2011 12:30:00 PM UTC in Programming

Edit 2011-12-10: This article is compatible with the final version of Silverlight 5 (5.0.61118.0).

This post is part of a mini series about Silverlight 5:

Having multiple windows is a common thing in desktop applications, especially in business environments. With Silverlight being positioned as a LOB platform, it was only logical that at some point we would see a feature of multiple windows in Silverlight too.

The situation so far

Up to Silverlight 4, out of browser applications (and obviously in-browser applications too) were restricted to one operating system window. This somewhat complicated certain scenarios where you would want to have additional tool windows or do side-by-side comparisons of multiple data items or similar. You had to design your application in a way that everything happens within the main window (think MDI style) and couldn't break out of the bounds of this main window (e.g. Silverlight pop-ups and child windows could not be moved outside of that window bounds).

What's new in Silverlight 5

In the next Silverlight version you will be able to create as many new windows as you need. These windows are fully integrated with the operating system, for example you will see them in the taskbar in Windows.

Note: this feature is only available to trusted out-of-browser applications.

Creating a window is pretty simple:

// create a new window
Window wnd = new Window();
wnd.WindowState = WindowState.Normal;
wnd.WindowStyle = WindowStyle.None;
wnd.Visibility = Visibility.Visible;
wnd.Content = new CustomContent();
wnd.Width = 640;
wnd.Height = 480;
wnd.Left = 100;
wnd.Top = 50;

As you can see you have a set of properties available you can use to manipulate the new window, like the window state and style as well as sizing and positioning options. One interesting part is the "Content" property, which is of type FrameworkElement. This is how you set the actual content of your new window. Typically this would be a user control or custom control, so you are really free to design the window content as you like. You can also set the window style to "None" like in the example above, which removes the operating system chrome altogether. This allows completely custom skinned windows, but then of course you need to implement things like a title bar or close buttons etc. yourself. The following shows a screenshot of 10 borderless mini windows that are all independent from each other. The main window is the upper right one:

image

You do not need to keep a reference to a newly created window in your own code to keep it alive (of course you can still do that if you need to access the window after creation). As soon as the constructor is executed in the code sample above, the window is added to the new "Windows" property of the application, which is of type WindowCollection. The first window in this collection always is the main window.

Window mainWindow = Application.Current.Windows
    .OfType<Window>()
    .ElementAt(0);

You can close windows using the respective method. If you close the main window, all other windows are closed too.

Limitations

Getting access to the parent window

At the moment, there doesn't seem to be a reliable way to get from a user control to the containing window. For example, if you set the content of a new window like in the above sample, then in none of the events I've tried (Loaded, SizeChanged, LayoutUpdated etc.) I was able to get access to the parent window. Why would you need access to it? For example, when you implement your own chrome, you need to call methods like DragMove or DragResize on it so your new window supports these features. One way around that of course is to pass in a reference to the window from the outside, but it would be nicer to have direct access to the parent.

public Window ParentWindow
{
    get { return _parentWindow; }
    set
    {
        // work around to get access to the parent window
        // to add drag functionality
        _parentWindow = value;
        MouseLeftButtonDown += (o, e) => _parentWindow.DragMove();
    }
}

Multi-monitor support

There is no real multi-monitor support. It's hard enough to get the current screen's resolution in Silverlight, but there's still no way to get information about the number of screens and their layout (or at least virtual desktop dimensions etc.), or to determine which monitor you're currently running on. Silverlight has no Screen or SystemInformation classes like WinForms, and the SystemParameters class does not contain that information in Silverlight like it does in WPF.

It's possible that users move windows around manually to their different screens, and that you save and restore the window positions though.

No modal dialogs

There's no option to create modal dialogs. All newly created windows are non-blocking. This means that if you want to prevent the user from interacting with the rest of the application, you have to use the same mechanisms you had to use in Silverlight before, like disabling the rest of the UI while a dialog is visible.

Other

There are a few corners that still seem odd. For example, you can set the window style of the main window to "BorderlessRoundCornersWindow" to get a nice rounded corners effect. However if you try to set that value on additional windows you create, an exception will be thrown. This limitation is explicitly mentioned in the documentation here, but it's hard to understand why such a restriction exists. Another example is the TopMost property. The linked documentation claims that it is only meaningful for the main window; this is also stated in other places. However when you test it, you will see that it works just as expected for other windows you create too. You can even mix it and make some secondary windows TopMost and not others, without any problems.

Silverlight developers should have no problems living with most of the limitations in place; for example, the missing modal dialogs is the exact same programming model you were already used to in Silverlight applications before. However, some of the issues are annoying, in particular that you're not able to get information about multiple monitors or the virtual desktop size – that would complete the feature of having multiple windows in a useful and nice way.

Tags: Silverlight · Silverlight 5