Uno Platform에서 CustomControl 만들기

Uno CustomControl


WPF에서 자주 사용하는 CustomControl을 Uno에서도 구현이 가능합니다.

Uno Documentation…
https://platform.uno/docs/articles/guides/how-to-create-control-libraries.html

1. CustomControl생성


먼저 Custom Control 객체를 생성합니다.

public class MenuList : ListBox
{
    static MenuList ()
    {
        DefaultStyleKey = typeof(MenuList); 
    }
}

WPF와 비교했을 때 DefaultStyleKey 부분이 조금 달라졌습니다.

(DefaultStyleKeyProperty => DefaultStyleKey)

2. MergedDictionaries 추가


Generic.xaml파일은 자동으로 연결되지 않기 때문에 Uno에서는 반드시 App.xaml에서 수동으로 추가해야 합니다.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ms-appx:///Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>

3. ControlTemplate 정의


마지막으로 CustomControl의 스타일 템플릿을 정의합니다.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:units="clr-namespace:jamesblog.UI.Units">

    <Style TargetType="units:MenuList">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="units:MenuList">
                    <Grid>
                        <ItemsPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

전체적으로는 WPF와 매우 비슷하지만 문법이 조금씩 다르기 때문에 사소한 문제로 인해 실행이 안될지도 모릅니다.

그래서 비교가 가능하도록 Uno에서 정상적으로 동작하는 샘플 소스도 함께 첨부합니다!!

읽어주셔서 감사합니다.
:smile:

2개의 좋아요