--- title: "How to: Get and Set the Main Application Window" description: Follow this example to get and set the main application window within Windows Presentation Foundation (WPF) application. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "windows objects [WPF], setting" - "setting windows objects [WPF]" - "windows objects [WPF], getting" - "getting windows objects [WPF]" ms.assetid: ec902bc4-4a59-46f5-8ec1-963b46789356 --- # How to: Get and Set the Main Application Window This example shows how to get and set the main application window. ## Example The first that is instantiated within a Windows Presentation Foundation (WPF) application is automatically set by as the main application window. The first to be instantiated will most likely be the window that is specified as the startup uniform resource identifier (URI) (see ). The first could also be instantiated using code. One example is opening a window during application startup, like the following: [!code-csharp[HOWTOWindowManagementSnippets#FirstWindowUsingCodeCODEBEHIND](~/samples/snippets/csharp/VS_Snippets_Wpf/HOWTOWindowManagementSnippets/CSharp/App.xaml.cs#firstwindowusingcodecodebehind)] [!code-vb[HOWTOWindowManagementSnippets#FirstWindowUsingCodeCODEBEHIND](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HOWTOWindowManagementSnippets/visualbasic/application.xaml.vb#firstwindowusingcodecodebehind)] Sometimes, the first instantiated is not actually the main application window e.g. a splash screen. In this case, you can specify the main application window using markup, like the following: [!code-xaml[ApplicationMainWindowSnippets#SetApplicationMainWindowXAML](~/samples/snippets/xaml/VS_Snippets_Wpf/ApplicationMainWindowSnippets/XAML/App.xaml#setapplicationmainwindowxaml)] Whether the main window is specified automatically or manually, you can get the main window from using the following code, like the following: [!code-csharp[ApplicationMainWindowSnippets#GetApplicationMainWindowCODE](~/samples/snippets/csharp/VS_Snippets_Wpf/ApplicationMainWindowSnippets/CSharp/App.xaml.cs#getapplicationmainwindowcode)] [!code-vb[ApplicationMainWindowSnippets#GetApplicationMainWindowCODE](~/samples/snippets/visualbasic/VS_Snippets_Wpf/ApplicationMainWindowSnippets/visualbasic/application.xaml.vb#getapplicationmainwindowcode)]