从 .NET MAUI 中的 viewModel 进行数据绑定的搜索处理程序

问题描述 投票:0回答:1

我有一个 .NET MAUI 移动应用程序,我正在尝试实现一个带有来自

SearchHandler
的数据绑定的
ViewModel

我有下面的代码,但我收到“没有为“客户端”找到属性、BindableProperty 或事件,或值和属性之间的类型不匹配”错误。

ClientPage.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:Realizer.ViewModels"
             xmlns:sh="clr-namespace:Realizer.Resources.SearchHandlers"
             xmlns:models="clr-namespace:Realizer.Models"
             x:DataType="vm:ClientsViewModel"
             x:Class="Realizer.Pages.ClientsPage"
             Title="Clients">
    
    <Shell.SearchHandler>
        <sh:ClientSearchHander ShowsResults="True"
                               Clients="{Binding Clients}"
                               DisplayMemberName="Name">
            
        </sh:ClientSearchHander>
    </Shell.SearchHandler>

ClientSearchHandler.cs

namespace Realizer.Resources.SearchHandlers
{
    public class ClientSearchHandler : SearchHandler
    {
        //public static readonly BindableProperty Clients;
        public ObservableCollection<Client> Clients { get; set; }
        protected  override void OnQueryChanged(string oldValue, string newValue)
        {
            base.OnQueryChanged(oldValue, newValue);
            if(string.IsNullOrWhiteSpace(newValue))
            {
                ItemsSource = null;
            }
            else
            {
                ItemsSource = Clients.Where(x => x.Name.Contains(newValue)).ToList();
            }
        }

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

ClientsViewModel.cs

public partial class ClientsViewModel : ObservableObject
{
        [ObservableProperty]
        private ObservableCollection<Client> _clients = new();
       
        public async Task LoadClientsAsync()
        {
           var clients = await _context.GetAllAsync<Client>();

           if (clients is not null && clients.Any())//if we have at least one client
           {
               Clients ??= new ObservableCollection<Client>(); //if null, initialize

               foreach (var client in clients)
               {
                   Clients.Add(client);
               }
           }
         }
}

我看到有人问同样的问题here并尝试在

Clients
ClientSearchHandler.cs
中制作
BindableProperty
,正如我评论的那样。但当我设置
Client.Where(...)
说“‘BindableProperty’不包含‘Where’的定义时,它发生了冲突。”

c# xaml search data-binding maui
1个回答
0
投票

正如 Jason 所说,您可以为 Clients 属性创建 BindableProperty。我已经测试过它并使其工作。

ClientSearchHandler.cs:

public class ClientSearchHandler : SearchHandler
    {
        public static readonly BindableProperty ClientsProperty
            = BindableProperty.Create("Clients", typeof(ObservableCollection<Client>), typeof(ClientSearchHandler), null);
        public ObservableCollection<Client> Clients
        {
            get=>(ObservableCollection<Client>)GetValue(ClientsProperty);
            set=>SetValue(ClientsProperty,value);
        }
        protected override void OnQueryChanged(string oldValue, string newValue)
        {
            base.OnQueryChanged(oldValue, newValue);
            if (string.IsNullOrWhiteSpace(newValue))
            {
                ItemsSource = null;
            }
            else
            {
                ItemsSource = Clients.Where(x => x.Name.Contains(newValue)).ToList();
            }
        }
       
        protected override void OnItemSelected(object item)
        {
            base.OnItemSelected(item);
        }
    }

结果图像:

如果您添加了有关bindableproperty的代码,您可以尝试清理并重建项目来使用它。

© www.soinside.com 2019 - 2024. All rights reserved.