Silverlight 4 Rough Notes: Customising the Window

Note – these posts are put together after a short time with Silverlight 4 as a way of providing pointers to some of the new features that Silverlight 4 has to offer. I’m posting these from the PDC as Silverlight 4 is announced for the first time so please bear that in mind when working through these posts.

For an out-of-browser Silverlight 4 application, you can take more control around the Window that presents your application.

Firstly, by changing the XML manifest you can control where the Window positions itself on the screen, for example;

<OutOfBrowserSettings ShortName="SilverlightApplication24 Application" EnableGPUAcceleration="False" ShowInstallMenuItem="True">
  <OutOfBrowserSettings.Blurb>SilverlightApplication24 Application on your desktop; at home, at work or on the go.</OutOfBrowserSettings.Blurb>
  <OutOfBrowserSettings.WindowSettings>
    <WindowSettings Title="SilverlightApplication24 Application" 
                    WindowStartupLocation="Manual"
                    Left="0"
                    Top="0"
                    Width="640"
                    Height="480"/>
  </OutOfBrowserSettings.WindowSettings>
  <OutOfBrowserSettings.Icons />
</OutOfBrowserSettings>

Here, not only are we specifying Width and Height but we’ve also specified the WindowStartupLocation to be Manual ( rather than CenterScreen ) and we’ve then specified Top and Left to position the window right where we want it.

Having put our window into a particular place, we can also resize it after the application has loaded – that is we can write code such as;

<UserControl x:Class="SilverlightApplication24.MainPage"
    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="White">
        <Button
            Tag="W"
            Content="Wider"
            Click="OnChangeSize"
            HorizontalAlignment="Right"
            VerticalAlignment="Center"
            RenderTransformOrigin="0.5,0.5">
            <Button.RenderTransform>
                <RotateTransform
                    Angle="90" />
            </Button.RenderTransform>
        </Button>
        <Button
            Tag="N"
            Content="Narrower"
            Click="OnChangeSize"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            RenderTransformOrigin="0.5,0.5">
            <Button.RenderTransform>
                <RotateTransform
                    Angle="-90" />
            </Button.RenderTransform>
        </Button>
        <Button
            Tag="S"
            Click="OnChangeSize"
            Content="Shorter"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"/>
        <Button
            Tag="T"
            Click="OnChangeSize"
            Content="Taller"
            HorizontalAlignment="Center"
            VerticalAlignment="Bottom"/>
    </Grid>
</UserControl>

with a little code behind which resizes the window on the button presses;

  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();      
    }
    private void OnChangeSize(object sender, RoutedEventArgs e)
    {
      // hack
      switch ((string)(((Button)sender).Tag))
      {
        case "N":
          Application.Current.MainWindow.Width -= 96;
          break;
        case "W":
          Application.Current.MainWindow.Width += 96;
          break;
        case "T":
          Application.Current.MainWindow.Height += 96;
          break;
        case "L":
          Application.Current.MainWindow.Height -= 96;
          break;
      }
    }
  }

I initially tried to do that via data-binding and it didn’t work out for me – I need to revisit that to figure out why.

It’s also possible to bring the MainWindow to the front with Activate() and to check whether it is active with IsActive.

It’s also possible to control whether the window tries to always be above other windows with the TopMost property and to control the WindowState of the window ( maximised/minimised/normal ) but those two things can only be done if the application is running elevated.