coding

APPLICATION / WINDOW

유로파물고기 2021. 5. 14. 20:36
반응형

// System.Windows.Forms: 윈폼
// System.Window 에서 구현하는 경우:  WPF

WPF 실행을 위해 필요한 2개 클래스

Application, Window

 

1. Application 

// MyApp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; // Application

namespace ch03_1
{
    class MyApp: Application // Application 상속
    {
        [STAThread] // 1. 싱글 쓰레드로 시작 됨
        public static void Main()
        {
            MyApp App = new MyApp();
            App.Startup += InitializeComponent; 
            App.Run(); // 앱 실행
        }

        private static void InitializeComponent(object sender, StartupEventArgs e)
        {
            win w = new win();
            w.Show();
        }

    }
}

2. Windows

partial class : 다른 파일에 같은 클래스 관리

// win.2.cs
using System.Windows;
using System.Windows.Controls;

namespace ch03_1
{
    public partial class win : Window
    { 
        public win(){
            InitializeComponent();
        }
        public void InitializeComponent()
        {
            Button btn = new Button();
            btn.Content = "click me";
            btn.Width = 300;
            btn.Height = 300;
            btn.Click += Btn_Click;

            this.Title = "hello";
            this.Content = btn;
            this.Show();
        }
    }
}
// win.cs
// 이벤트 처리
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ch03_1
{
    public partial class win : Window
    {
        private static void Btn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Clicked");
        }

    }
}

 

WPF 앱은 이런 원리로 실행이 된다고 한다.

'coding' 카테고리의 다른 글

버전간의 차이점 비교~ 수업을 마치며  (0) 2022.08.03
sqlite3 사용하기  (0) 2021.07.27
GUI tkinter Label, Button  (0) 2021.05.13
Kotlin 기초정리2  (0) 2021.04.03
Kotlin 기초1 정리  (0) 2021.03.18