■ Shell 엘리먼트의 SearchHandler 첨부 속성을 사용해 셸 검색을 사용하는 방법을 보여준다.
▶ AnimalSearchHandler.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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using TestProject.Models; namespace TestProject; /// <summary> /// 동물 검색 핸들러 /// </summary> public class AnimalSearchHandler : SearchHandler { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리스트 /// </summary> private List<AnimalModel> list = new List<AnimalModel> { new AnimalModel { ID = 1, Name = "고양이", Route = "//고양이" }, new AnimalModel { ID = 2, Name = "개" , Route = "//개" }, new AnimalModel { ID = 3, Name = "원숭이", Route = "//원숭이" }, new AnimalModel { ID = 4, Name = "코끼리", Route = "//코끼리" }, new AnimalModel { ID = 5, Name = "곰" , Route = "//곰" } }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 쿼리 변경시 처리하기 - OnQueryChanged(oldValue, newValue) /// <summary> /// 쿼리 변경시 처리하기 /// </summary> /// <param name="oldValue">이전 값</param> /// <param name="newValue">신규 값</param> protected override void OnQueryChanged(string oldValue, string newValue) { base.OnQueryChanged(oldValue, newValue); if(string.IsNullOrWhiteSpace(newValue)) { ItemsSource = null; } else { ItemsSource = this.list; } } #endregion #region 항목 선택시 처리하기 - OnItemSelected(item) /// <summary> /// 항목 선택시 처리하기 /// </summary> /// <param name="item">항목</param> protected override async void OnItemSelected(object item) { base.OnItemSelected(item); AnimalModel animal = item as AnimalModel; await Shell.Current.GoToAsync($"{animal.Route}"); } #endregion } |
▶ CatPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.CatPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestProject" Title="Cat Page" BackgroundColor="White"> <Shell.SearchHandler> <local:AnimalSearchHandler DisplayMemberName="Name" Placeholder="동물명을 입력해 주시기 바랍니다." /> </Shell.SearchHandler> <StackLayout> <Label Text="CAT PAGE" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" /> </StackLayout> </ContentPage> |