728x90

EventToCommandBehavior 는 컨트롤의 이벤트를 Command 로 연결해줍니다.
아래 내용은 Entry 에 focus 된 경우 Toast 메세지를 표시합니다.

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="Maui.ToolKitMaui.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:animations="clr-namespace:Maui.ToolKitMaui.Animations"
    xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="15"
            VerticalOptions="Center">

            <Entry
                BackgroundColor="LightGray"
                Placeholder="Event To Command (Focused)"
                PlaceholderColor="White"
                TextColor="Black">
                <Entry.Behaviors>
                    <mct:EventToCommandBehavior Command="{Binding EventToCommand}" EventName="Focused" />
                </Entry.Behaviors>
            </Entry>
            
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

MainViewModel.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

using CommunityToolkit.Maui.Alerts;

namespace Maui.ToolKitMaui
{
    public partial class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public ICommand EventToCommand => new Command(() => OnEventToCommand());

        private void OnEventToCommand()
        {
            Toast.Make("Event To Command").Show();
        }
    }
}


실행결과

[Source]
https://github.com/kei-soft/KJunBlog/tree/master/Maui.ToolKitMaui

728x90
Posted by kjun.kr
,