我正在使用Prism开发Xamarin表单应用程序。我正在尝试使用动态数据来处理(排序、筛选等(和显示分组数据。我在关注这个https://www.xamboy.com/2021/01/20/using-dynamic-data-in-xamarin-forms-part-1/
我的问题是我的Readonlyobservablecollection_getUsers没有被填充,请参阅以下内容:
private SourceCache<User, string> _sourceCache = new SourceCache<User, string>(x =>x.Id);
private ReadOnlyObservableCollection<ObservableGroupedCollection<string,User, string>> _getUsers;
public ReadOnlyObservableCollection<ObservableGroupedCollection<string, User, string>> GetUser { get; set; }
public string Gender { get { return _gender; } set { SetProperty(ref _gender, value); OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(nameof(Gender))); } }
private string _gender;
private async Task GetUsers()
{
try
{
var users = await _apiService.GetUsers();
_sourceCache.AddOrUpdate(users);
Func<User, bool> searchFilter(string text) => user =>
{
return string.IsNullOrWhiteSpace(text) || user.PhoneNumber.ToLower().Contains(text.ToLower()) || user.UserName.ToLower().Contains(text.ToLower()) || user.Name.ToLower().Contains(text.ToLower());
};
Func<User, bool> genderFilter(string gender) => user =>
{
return gender == "All" || user.Gender == gender;
};
Func<User, bool> statusFilter(string status) => user =>
{
if(status == "Live")
{
return user.Active == true;
}
if (status == "Suspended")
{
return user.Active == false;
}
return status == "None" || user.Payment.StatusMessage.Replace(" ","") == status;
};
var genderPredicate = this.WhenAnyValue(x => x.Gender)
.Select(genderFilter);
var filterPredicate = this.WhenAnyValue(x => x.SearchText)
.Select(searchFilter);
var statusPredicate = this.WhenAnyValue(x => x.Status)
.Select(statusFilter);
var sortPredicate = this.WhenAnyValue(x => x.SortBy)
.Select(x => SortExpressionComparer<User>.Ascending(a => a.Name));
_cleanUp = _sourceCache.Connect()
.Filter(genderPredicate)
.Group(x => x.Race)
.Transform(g => new ObservableGroupedCollection<string, User, string>(g, genderPredicate, sortPredicate))
.Bind(out _getUsers)
.DisposeMany()
.Subscribe();
Gender = "All";
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(nameof(Gender)));
}
catch (Refit.ApiException ex)
{
if(ex.StatusCode != System.Net.HttpStatusCode.NotFound)
{
await HandleExceptionAsync(ex);
}
}
catch (Exception ex)
{
await HandleExceptionAsync(ex);
}
}
通用类
public class ObservableGroupedCollection<TGroupKey, TObject, TKey> : ObservableCollectionExtended<TObject>, IDisposable
{
public TGroupKey Key { get; }
public ObservableGroupedCollection(IGroup<TObject, TKey, TGroupKey> group, IObservable<Func<TObject, bool>> filter, IObservable<IComparer<TObject>> comparer)
{
this.Key = group.Key;
//load and sort the grouped list
var dataLoader = group.Cache.Connect()
.Filter(filter)
.Sort(comparer)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(this) //make the reset threshold large because xamarin is slow when reset is called (or at least I think it is @erlend, please enlighten me )
.Subscribe();
_cleanUp = new CompositeDisposable(dataLoader);
}
public void Dispose()
{
_cleanUp.Dispose();
}
private readonly IDisposable _cleanUp;
}
我真的很挣扎。如果有人能帮忙,我真的很感激。
您可以使用它来填充_getUsers
。
public ReadOnlyObservableCollection<ObservableGroupedCollection<string, User, string>> GetUser {
get
{
return _getUsers;
}
set
{
if (_getUsers != value)
{
_getUsers = value;
OnPropertyChanged(nameof(GetUser));
}
}
}