■ Shell 엘리먼트의 BackButtonBehavior 속성을 사용해 뒤로가기 버튼 동작을 설정하는 방법을 보여준다.
▶ MonkeyDetailViewModel.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 |
using System.Windows.Input; namespace TestProject.ViewModels; /// <summary> /// 원숭이 상세 뷰 모델 /// </summary> public class MonkeyDetailViewModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 뒤로가기 버튼 명령 - BackButtonCommand /// <summary> /// 뒤로가기 버튼 명령 /// </summary> public ICommand BackButtonCommand { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MonkeyDetailViewModel() /// <summary> /// 생성자 /// </summary> public MonkeyDetailViewModel() { BackButtonCommand = new Command ( () => { Shell.Current.Navigation.PopAsync(); } ); } #endregion } |
▶ MonkeyDetailPage.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 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MonkeyDetailPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodel="clr-namespace:TestProject.ViewModels" Title="Monkey Detail Page" BackgroundColor="White"> <ContentPage.BindingContext> <viewmodel:MonkeyDetailViewModel /> </ContentPage.BindingContext> <Shell.BackButtonBehavior> <BackButtonBehavior IconOverride="dotnet_bot.png" Command="{Binding BackButtonCommand}" /> </Shell.BackButtonBehavior> <StackLayout> <Label x:Name="label" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Text="MONKEY DETAIL PAGE" /> </StackLayout> </ContentPage> |