WPF

[WPF] WPF Application(2) - App.xaml 과의 연동

귀뚜래미 2022. 9. 6. 15:19
728x90

 

- App.xaml은 어플리케이션의 선언부 시작점이다.

- VS에서 새로운 프로젝트 생성 시 App.xaml.cs라는 코드 비하인드 파일을 자동으로 생성함과 동시에 이 파일도 생성된다.

- 두 개의 파일이 부분 클래스로 함께 작동하며 XAML과 코드 비하인드에서 모두 작업할 수 있도록 한다.

- App.xaml.cs는 윈도우 애플리케이션의 중심 클래스인 Apllication 클래스를 확장한다.

- .NET은 이 클래스에서 명령들을 실행하고 요청한 윈도우 또는 페이지를 시작한다.

 

App.xaml의 구조

새로운 애플리케이션을 생성하면 자동으로 다음과 같은 App.xaml이 생성된다.

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

- StartupUri : 애플리케이션이 실행될 때 어떤 윈도우 또는 페이지로 시작하는지 정의하는 부분이다. 위의 경우 MainWindow.xaml이 해당된다.

- 다양한 윈도우 디스플레이 방식에 대해 StartupUri 속성 값 대신 코드 비하인드에서 작업도 가능하다.

 

 

App.xaml.cs의 구조

using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
	public partial class App : Application
	{

	}
}

- Startup 이벤트를 감지하면 수동으로 시작 윈도우를 생성할 수 있다.

예시

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 Startup="Application_Startup">
    <Application.Resources></Application.Resources>
</Application>

- StartupUri를 Startup 이벤트 구독으로 대체한다. 코드 비하인드에서 해당 이벤트를 다음과 같이 사용 가능하다.

using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
	public partial class App : Application
	{

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			// Create the startup window
			MainWindow wnd = new MainWindow();
			// Do stuff here, e.g. to the window
			wnd.Title = "Something else";
			// Show the window
			wnd.Show();
		}
	}
}

 

728x90

'WPF' 카테고리의 다른 글

[WPF] WPF Application(4) - Application Culture / UICulture  (0) 2022.09.06
[WPF] WPF Application(3) - 리소스  (0) 2022.09.06
[WPF] WPF Application(1) - Window 클래스  (0) 2022.09.01
[WPF] XAML  (0) 2022.09.01
[WPF] WPF 시작  (0) 2022.08.30