Silverlight 5–What’s New #2

April 13, 2011

This is a second post in series of posts about new features in Silverlight 5.

This post focuses at simple, yet much desired feature of multiple windows support.

First – some clarification: multiple windows will work in elevated out-of-browser mode only.

To demonstrate this feature I’ve built simplest application which spans 5 additional windows on button click.

The “child” window presents a UserControl. Child window content:

<UserControl x:Class="MultipleWindows_OOB.ChildWindowContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="LightGoldenrodYellow">
        <TextBlock HorizontalAlignment="Center" TextAlignment="Center" 
                   FontFamily="Courier New" FontSize="22">
            <Run Text="This is a secondary window"/>
            <LineBreak/>
            <Run Text="#"/>
            <Run Text="{Binding WindowNumber}"/>
        </TextBlock>
    </Grid>
</UserControl>

The “WindowNumber” defined as Dependency Property in code behind as follows:

private int windowNumber;
public int WindowNumber 
{
    get { return windowNumber; }
    set
    {
        windowNumber = value;

        if (null != PropertyChanged)
            PropertyChanged(this, new PropertyChangedEventArgs("WindowNumber"));
    }
}

public event PropertyChangedEventHandler PropertyChanged;

Now to the interesting part – spawning a new windows:

if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
{
    for (int i = 0; i < 5; i++)
    {
        Window wnd = new Window();
        wnd.Width = 640;
        wnd.Height = 480;
        wnd.Left = 25 + i * 20;
        wnd.Top = 30 + i * 30;
        wnd.WindowState = WindowState.Normal;
        wnd.WindowStyle = WindowStyle.SingleBorderWindow;
        wnd.Title = "Child Window #" + (i + 1);
        wnd.Content = new ChildWindowContent() { WindowNumber = (i + 1) };
        wnd.Visibility = System.Windows.Visibility.Visible;
    }
}
else
    MessageBox.Show("This works only in Elevated OOB mode!");

Pretty simple, ah? At running application looks like the follows:

image

Note: closing the main window will close all the child windows.

The sample located here.

 

Stay tuned for more to come soon.

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. Tory Burch OutletSeptember 7, 2011 ב 10:46

    thanks for your sharing,I like it!

    Reply