.NET MAUI - XAML 기본

2022. 7. 5. 00:00MAUI

반응형

Xaml 에서 특정 형식을 인스턴스화 하는 방법

예) 특정 색상으로 레이블을 만들려는 경우 Xaml 은 다음과 같이 구현

<Label TextColor="AntiqueWhite"/>

이 Xaml 코드는 .NET MAUI Xaml Parser 에 의해 구분 분석된다. 

그리고 다음과 같은 c# 코드를 만들어 낸다. 

var myLabel = new Label
{
  TextColor = Color.FromRgb(255, 255, 100)
};

C# code 에서는 using 지시문을 사용하여 namespace 를 해당 코드 범위로 가져올수 있다.

Xaml page 에서는  page 의 xmlns 특성을 사용하여 namespace 를 참조할 수 있다.

 

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             ...>

    ...
</ContentPage>

http://schemas.microsoft.com/dotnet/2021/maui 
URI 형식의 namespace 이며  페이지의 기본 namespace 이다. 

Microsoft.Maui Nuget pagkage 의 assembly 에 정의된 네임스페이스들의 별칭이다.

시작 부분에 이 네임스페이스를 지정하면 모든 .NET MAUI 형식 및 컨트롤이 범위에 포함된다.

이 네임스페이스를 생략하면 Button, Label, Entry 또는 StackLayout과 같은 컨트롤을 사용할 수 없다.

 

http://schemas.microsoft.com/winfx/2009/xaml 
문자열, 숫자 및 속성과 같은 다양한 .NET 내장 형식이 포함된 어셈블리를 참조한다.

 

xmlns:x  에서 x: 를 접두사로 사용하여 네이스페이스 형식을 참조한다.

<ContentPage ...
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiXaml.Page1"
            ...>

    ...
</ContentPage>

또한 프로젝트의 특정 namespace 에 Xaml 에서 사용하려는 class 가 있는 경우 아래와 같이 한다. 

clr-namespace:<특정 NameSpace>

예) Utils 라는 namespace 범위에 구현된 class 들을 사용하려면 (mycode 를 활용하면 된다.)

<ContentPage ...
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mycode="clr-namespace:Utils"
            ...>

    ...
</ContentPage>

Type Converter 란 무엇인가?

Type Converter는 문자열 값으로 지정된 XML 특성을 올바른 Type으로 변환하는 데 사용됩니다.

<Label Text="Username" TextColor="Black" FontSize="42" FontAttributes="Bold,Italic" />

이 코드는 Text, TextColor, FontSize, FontAttributes 속성을 설정 한다. 

Text 는 문자열 속성이므로 Type Converter 는 필요 없다. 

TextColor 는 Color 형식이다. 그러므로 Xaml 은 문자열을 해당 Color 로 변환하는 Converter 가 필요하다.

FontSize 는 정수형식 이다. 마찬가지로 Converter 가 필요하다.

FontAttributes 는 복잡한 형식(Complex type)이다. 

  • "Bold, Italic" 문자열로 결합할 수 있다.
  • 쉼표로 구분된 문자열은 [Flags] 기반 열거로 처리된다.
  • 적절한 형식 변환기는 값의 비트 단위 OR을 속성에 적용한다.

복잡한 형식 할당 (Complex type assignment)

만약 Label 안에  Tap 기능을 처리 하게 하려고 한다면 다음과 같이 할 수 있다. 

<Label Text="Username" TextColor="Black" FontSize="42" FontAttributes="Bold,Italic">
    <Label.GestureRecognizers>
        <TapGestureRecognizer NumberOfTapsRequired="2" />
    </Label.GestureRecognizers>
</Label>

이러한 방식을 Property Element (Type.PropertyName) 라고 한다. 

 

Default content property

<VerticalStackLayout>
    <VerticalStackLayout.Children>
        <Label Text="Please log in" />
    </VerticalStackLayout.Children>
</VerticalStackLayout>

위의 코드에서 <VerticalStackLayout.Children>    의 Children 속성은 기본 속성이다.

즉 .Children 을 생략 하고 아래와 같이 코드를 구성해도 같은 동작을 하게 된다. 

<VerticalStackLayout>
    <Label Text="Please log in" />
</VerticalStackLayout>

 

관련영상

https://youtu.be/RTgKeMWHmFU

 

반응형

'MAUI' 카테고리의 다른 글

.NET MAUI - XAML 마크업 확장 (mark-up extensions)  (0) 2022.07.07
.NET MAUI - XAML 이벤트 핸들링  (0) 2022.07.06
.NET MAUI - XAML 의 동작방식  (0) 2022.07.04
.NET MAUI - Controls and Layouts  (0) 2022.07.01
.NET MAUI - Add page content  (0) 2022.06.30