지식 창고

기초 지식 - 2 본문

프로그래밍/WPF

기초 지식 - 2

Lucky-John 2021. 12. 24. 17:06

https://hola-que-tal.tistory.com/35?category=825575 

 

C# Winform WPF #2

안녕하세요~! 문쑹입니다 :) XMAL 이론 XAML 네임스페이스 이해 MainWindow.xaml 소스 코드 객체 생성 방법 new Car(); // Car 클래스 선언 using System.Windows.Media; namespace BusinessLogic { public class..

hola-que-tal.tistory.com

 

XMAL 이론

XAML 네임스페이스 이해

 

MainWindow.xaml 소스 코드

<Window x:Class="BikeShop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BikeShop"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  <Grid>
  	<Frame x:Name="MainFrame" Source="/BikeShop;component/Contact.xaml" NavigationUIVisibility="Hidden"/>
  </Grid>

 

객체 생성 방법

new Car(); <!-- C#에서의 객체 생성 -->
<Car />	   <!-- XAML에서 객체 생성 -->
// Car 클래스 선언
using System.Windows.Media;
namespace BusinessLogic
{
    public class Car
    {
        public double Speed{get;set;}
        public ColorColor {get;set;}
    }
}

// C# 객체 생성
using BusinessLogic;
new Car();

 

XAML에서 객체 생성

<Label xmlns:bl="clr-namespace:BusinessLogic">
	<bl:Car />
</Label>

 

생성한 객체의 속성 값 할당

/* ====== C# ====== */ 
var c=new Car();
c.Speed =100;
c.Color =Colors.Red;
<!-- ====== XAML ====== -->
<Label xmlns:bl="clr-namespace:BusinessLogic">
	<bl:Car Speed="100" Color="Red"/>
</Label>

 

확장

public class Human 
{
    public string FirstName {get; set;}
    public bool HasDrivingLicense {get; set;} 
 }
 
public class Car 
{
    public double Speed {get; set;}
    public ColorColor {get; set;}
    public HumanDriver {get; set;} 
}

 

아래의 코드는 C# 코드와 XMAL 코드이지만 내용은 동일하다.

var h=new Human();
h.FirstName ="Nick";
h.HasDrivingLicense =true;

var c=new Car();
c.Speed =100;
c.Color =Colors.Red;
c.Driver =h;
<Label xmlns:bl="clr-namespace:BusinessLogic">
  <bl:Car Speed="100" Color="Red">
    <bl:Car.Driver>
    	<bl:Human FirstName="Nick" HasDrivingLicense="True"/>
    </bl:Car.Driver>
  </bl:Car>
</Label>

 

명명 규칙

XAML요소를 참조할 때

xmlns:bl="clr-namespace:BusinessLogic"
mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"
Title="Test">

<Grid>
  <Label> 
  	<bl:Car x:Name="myCar" Speed="100" Color="Red"/>
  </Label>
</Grid>

C#에서 사용할 때

private void Inits()
{
	myCar.Color =Colors.Blue;
}

 

이벤트

코드 비하인드 메서드와 연계

<Button Click="Greet"/>
private void Greet(object sender,RoutedEventArgs e)
{
	MessageBox.Show("Hell");
}

 

'프로그래밍 > WPF' 카테고리의 다른 글

기초지식 - 4  (0) 2021.12.24
기초지식 - 3  (0) 2021.12.24
기초 지식 - 1  (0) 2021.12.24
이미지 등록과 이미지 나타내는 방법  (0) 2021.12.23
WPF에서 C++ DLL에 콜백함수 등록하기 (C++/CLR 사용)  (0) 2021.12.16
Comments