Home > database >  How to solve MAUI Shell.SearchHandler crashes when running on IOS environment?
How to solve MAUI Shell.SearchHandler crashes when running on IOS environment?

Time:12-19

In my Project I have setup a Shell.SearchHandler, it works perfectly when I run on Android Emulator but crashes when I trying to type something in the search bar when I running on IOS Emulator. It keeps pointing system null reference and crash immediately. Any solution for this?

StudentModel.cs


    public class StudentModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

StudentListViewModel.cs


    public class StudentListViewModel
    {
        public static List<StudentModel> Students { get; private set; } = new List<StudentModel>();

        public StudentListViewModel()
        {
            Students.Add(new StudentModel
            {
                Name = "asd",
                Email = "[email protected]",
            });
            Students.Add(new StudentModel
            {
                Name = "qwe",
                Email = "[email protected]",
            });
            Students.Add(new StudentModel
            {
                Name = "ert",
                Email = "[email protected]",
            });
        }
    }

StudentListView.Xaml


    <Shell.SearchHandler>
        <searchHandlers:StudentSearchHandler
            DisplayMemberName="Name"
            Students="{x:Static vm:StudentListViewModel.Students}">


        </searchHandlers:StudentSearchHandler>

    </Shell.SearchHandler>

StudentSearchHandler.cs

    public class StudentSearchHandler : SearchHandler
    {
        public IList<StudentModel> Students { get; set; }
        protected override void OnQueryChanged(string oldValue, string newValue)
        {
            base.OnQueryChanged(oldValue, newValue);

            if (string.IsNullOrWhiteSpace(newValue))
            {
                ItemsSource = null;
            }
            else
            {
                ItemsSource = Students.Where(student => student.Name.ToLower().Contains(newValue.ToLower())).ToList();
            }
        }

        protected override async void OnItemSelected(object item)
        {
            base.OnItemSelected(item);

        }
    }

How to solve this error? Thanks. I tried to set a breakpoint to test on different emulators, Android emulator will execute protected override void OnQueryChanged(string oldValue, string newValue){} method, but when running on an IOS emulator will not go into this method.

CodePudding user response:

That's a known issue on .NET Maui. You could follow this issue [iOS] Shell.SearchHandler crashes the app .

Additionally, you might use SearchBar as an alternative. For more info, you could refer to .NET Maui SearchBar.

  • Related