■ x:Type 태그 확장를 사용하는 방법을 보여준다.
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackLayout Grid.Row="0" Margin="10" HorizontalOptions="Center"> <Button HorizontalOptions="Center" Text="Slider 객체 생성" CommandParameter="{x:Type Slider}" Command="{Binding CreateCommand}" /> <Button Margin="0,10,0,0" HorizontalOptions="Center" Text="Stepper 객체 생성" CommandParameter="{x:Type Stepper}" Command="{Binding CreateCommand}" /> <Button Margin="0,10,0,0" HorizontalOptions="Center" Text="Switch 객체 생성" CommandParameter="{x:Type Switch}" Command="{Binding CreateCommand}" /> </StackLayout> <ScrollView Grid.Row="1"> <StackLayout x:Name="stackLayout" /> </ScrollView> </Grid> </ContentPage> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System.Windows.Input; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성 명령 - CreateCommand /// <summary> /// 생성 명령 /// </summary> public ICommand CreateCommand { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); CreateCommand = new Command<Type> ( (Type viewType) => { View view = (View)Activator.CreateInstance(viewType); view.Margin = new Thickness(10); view.HorizontalOptions = LayoutOptions.Fill; this.stackLayout.Add(view); } ); BindingContext = this; } #endregion } |