我有一个.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' does not contain a definition for 'Where'”。
1条答案
按热度按时间2jcobegt1#
正如Jason所说,您可以为Clients属性创建BindableProperty。我已经测试过它并使其工作。
客户端搜索引擎.cs:
字符串
结果图像:
的数据
如果您已经添加了bindableproperty的代码,可以尝试清理和重建项目来使用它。