본문 바로가기

프로그래밍/안드로이드-자마린폼즈

EventToCommandBehavior in Prism

 

TabbedPageView.xaml

xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"

<TabbedPage.Behaviors>
        <b:EventToCommandBehavior EventName="ChildAdded"
                                  Command="{Binding CmdBack}"
                                  ></b:EventToCommandBehavior>
        <b:EventToCommandBehavior EventName="Appearing"
                                  Command="{Binding CmdBack}"
                                  ></b:EventToCommandBehavior>
</TabbedPage.Behaviors>

Prism MVVM 에서 컨트롤들의 이벤트를 command로 바인딩 시키는 방법이다.

위의 예제에서는 TabbedPage 컨트롤의 ChildAdded 이벤트와 Appearing 이벤트를 command로 바인딩 시켰다.

 

해당하는 커맨드는 아래와 같이 ViewModel에 구현된다.

 

TabbedPageViewModel.cs

using Prism.Commands;
using Prism.Navigation;
using System.ComponentModel;

namespace PrismTest.ViewModels
{  
    public class TabMainViewModel : ViewModelBase
    {
        #region command
        private DelegateCommand _cmdBack;
        public DelegateCommand CmdBack => _cmdBack ?? (_cmdBack = new DelegateCommand(Back, CanBack));

        private async void Back()
        {
        }

        private bool CanBack()
        {
            return true;
        }
        #endregion
    }
}