Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

Application.MainWindow Property

Gets or sets the main window of the application.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
'Declaration
Public Property MainWindow As Window
<object>
  <object.MainWindow>
    <Window .../>
  </object.MainWindow>
</object>

Property Value

Type: System.Windows.Window
A Window that is designated as the main application window.
ExceptionCondition
InvalidOperationException

MainWindow is set from an application that's hosted in a browser, such as an XAML browser applications (XBAPs).

MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain.

You can specify a different main window by setting MainWindow assigning another Windows object to the MainWindow property.

If the ShutdownMode property of the Application object is set to OnMainWindowClose, closing the main window causes the application to shut down.

It is possible to set the MainWindow property from XAML, if an application's main window is not the window that is produced by setting the StartupUri property in XAML. The two limitations of the XAML approach are:

  • You can specify either a XAML-only Window or a XAML-only NavigationWindow as the main window.

  • You must set the Visibility property of the window you specify, otherwise it won't be shown.

The reference to the first Window object to be instantiated is also added as the first item to the Windows collection. If MainWindow is subsequently set with a reference to a different Window, the position of the item with the reference to the main window will change, while the order of items in Windows remains the same. Consequently, always use MainWindow to refer to the main window instead of the first item in Windows.

NoteNote

If the main window is a NavigationWindow, and you need specific access to NavigationWindow members, you will need to cast the value of MainWindow to NavigationWindow.

This property is available only from the thread that created the Application object.

The following example shows how to find the main application window.

' Get the main window
Dim mainWindow As Window = Me.MainWindow

The following example shows how to set MainWindow using XAML.

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="StartupWindow.xaml"
    >
  <Application.MainWindow>
    <NavigationWindow Source="MainPage.xaml" Visibility="Visible"></NavigationWindow>
  </Application.MainWindow>
</Application>

The following example shows how to instantiate the MainWindow in code during application startup.

	Partial Public Class App
		Inherits Application
		Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
			Dim window As New MainWindow()
			window.Show()
		End Sub
	End Class

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.