본문 바로가기
Windows/C#/WPF

C# Framework 프로젝트에서 WPF 사용하는 예제

by hirudev 2021. 4. 28.
※ 해당 내용은 blog.okazuki.jp/entry/2014/12/27/200015 을 기반으로 공부하고 있는 내용입니다.

 

OnlyCSharpToXAML_Sample.zip
0.01MB

 

C# 프로젝트에서 WPF 를 사용하려면 아래의 4가지의 참조를 넣어주어야 한다.

 

  • PresentationCore
  • PresentationFramework
  • WindowsBase
  • System.Xaml

위 4가지를 참조 후

 

클래스를 새로 생성하여 아래와 같은 코드를 작성해본다.

 

   using System.Windows;
   using System.Windows.Controls;

    class Wpf:Window
    {
        private Button btn;
        private void InitializeComponent()
        {
            this.Title = "Hello world";
            this.Height = 480;
            this.Width = 360;

            btn = new Button
            {
                Content = "Click Me!",
                Width = 100,
                Height = 25
            };
            btn.Click += OnListener_Click;

            var grid = new Grid();

            grid.Children.Add(btn);

            this.Content = grid;

            this.Visibility = Visibility.Visible;
        }
        public Wpf()
        {
            this.InitializeComponent();
        }
        private void OnListener_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "test", "Click Me!");
        }
    }

 

이후 해당 클래스를 생성하면 아래와 같은 화면을 확인할 수 있다.

 

'Windows > C#/WPF' 카테고리의 다른 글

Object 요소 property  (0) 2021.05.01
XAML 의 로드  (0) 2021.04.30
WPF XAML 스타일 정의 예제  (0) 2021.04.29
WPF 의 구성  (0) 2021.04.28
App.g.cs 를 보는 방법  (0) 2021.04.27

댓글