xamarin.forms - 选择并从不同的复选框中获取多个数据
问题描述
我需要一些关于我的复选框和过滤项目的帮助。
我有 5 个复选框,每个复选框过滤不同的值。1 将根据类别将所有内容归还给我,其余的将归还给我。现在我需要用户能够选择多个类别。我能够获取每个类别的数据,但我不知道如何获取多个类别的数据。一旦用户完成了他的选择,他就会按下一个按钮,该按钮应该过滤项目并重定向到一个页面。
private static int? _userDiscount;
private static int _parentCategoryId;
private static List<Article> _allArticlesForPurchase;
private static List<Category> _allCategories;
private static List<CategoryGroup> _allUserCategoryGroups;
private static List<Article> _allUserArticles;
private static UserOrganization _userOrganization;
private bool _filterAllItems;
private bool _filterBeginnerItems;
private bool _filterIntermediateItems;
private bool _filterAdvancedItems;
private bool _filterUpperIntermediateItems;
public ICommand FilterItemsCommand { get; private set; }
public bool IsBusy { get; set; }
public bool FilterAllItems
{
set
{
_filterAllItems = value;
NotifyPropertyChanged();
if (_filterAllItems)
_parentCategoryId = -1;
FillArticles();
if (FilterAllItems == true)
{
FilterBeginnerItems = false;
FilterIntermediateItems = false;
FilterUpperIntermediateItems = false;
FilterAdvancedItems = false;
}
//var result = FilterArticlesForPurchase.Where(x => x.ArticleCategoryId == _parentCategoryId).ToList();
}
get => _filterAllItems;
}
public bool FilterBeginnerItems
{
set
{
_filterBeginnerItems = value;
NotifyPropertyChanged();
if (_filterBeginnerItems)
{
_parentCategoryId = 1;
FilterAllItems = false;
FillArticles();
}
}
get => _filterBeginnerItems;
}
public bool FilterIntermediateItems
{
set
{
_filterIntermediateItems = value;
NotifyPropertyChanged();
if (_filterIntermediateItems)
{
_parentCategoryId = 2;
FilterAllItems = false;
FillArticles();
}
}
get => _filterIntermediateItems;
}
public bool FilterUpperIntermediateItems
{
set
{
_filterUpperIntermediateItems = value;
NotifyPropertyChanged();
if (_filterUpperIntermediateItems)
{
_parentCategoryId = 3;
FilterAllItems = false;
FillArticles();
}
}
get => _filterUpperIntermediateItems;
}
public bool FilterAdvancedItems
{
set
{
_filterAdvancedItems = value;
NotifyPropertyChanged();
if (_filterAdvancedItems)
{
_parentCategoryId = 4;
FilterAllItems = false;
FillArticles();
}
}
get => _filterAdvancedItems;
}
public ObservableCollection<ArticleDetailData> FilterArticlesForPurchase { get; } = new ObservableCollection<ArticleDetailData>();
public FilterArticlesForPurchaseViewModel()
: base()
{
;
Task.Run(async () => await LoadAllDataForArticlesAndCategories()).Wait();
FilterItemsCommand = new Command(async () => await FilterItems());
}
private async Task LoadAllDataForArticlesAndCategories()
{
if (LangUpNetworkDelegate.IsOnline)
{
await FillCategories();
if (LangUpLoggedUser.LoggedIn)
{
await FillUsersArticles();
var userOrganizationApiResponse = await AVAT.App.ApiFactory.TryGetMainUserOrganization();
_userOrganization = userOrganizationApiResponse.MatchResult<UserOrganization>(default(UserOrganization), org => org);
if (_userOrganization != null &&
DateTime.ParseExact(_userOrganization.OrganizationPromocode.DateOfExpiration, "dd/MM/yyyy",
null) >= DateTime.Now)
{
_userDiscount = _userOrganization.OrganizationPromocode.DiscountPercentage;
}
}
else
{
await FillAnonymousArticles();
}
foreach (var group in _allUserCategoryGroups)
{
var listOfArticles = new List<Article>();
var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == group.Id).ToList();
foreach (var category in allGroupCategories)
{
var categoryArticles = _allUserArticles.Where(m => m.CategoryId == category.Id).ToList();
listOfArticles.AddRange(categoryArticles);
}
group.GroupArticles = listOfArticles;
}
FillArticles();
}
else
{
NotificationService.ShowToast("");
}
}
private async void FillArticles()
{
var allArticles = new List<Article>();
allArticles = FindAllArticlesForPurchase(allArticles);
var downloadedArticles = LangUpDataSaverLoader.DeserializeAllOptimizationData();
foreach (var article in allArticles)
{
var filename = string.Format(SharedConstants.ArticleImageUrl, SharedConstants.ApiBaseUri, article.Id);
var rating = article.FinalRate == -1 ? "-.-" : ((float)article.FinalRate).ToString("0.0");
if (downloadedArticles.DownloadedArticles.Any(m => m.Id == article.Id))
{
filename = article.Id.ArticleImageFile();
}
var detail = new ArticleDetailData()
{
Author = article.Author,
Avatar = filename,
BackgroundImage = filename,
Description = article.Description,
Body = article.Description,
Section = article.Category,
Id = article.Id,
Subtitle = article.Description,
Title = article.NameCz,
}
FilterArticlesForPurchase.Add(detail);
}
}
private static List<Article> FindAllArticlesForPurchase(List<Article> allArticles)
{
if (_parentCategoryId != -1)
{
foreach (var categoryGroup in _allUserCategoryGroups)
{
var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == categoryGroup.Id).ToList();
if (_parentCategoryId != -1)
{
foreach (var category in allGroupCategories)
{
if (category.ParentId == _parentCategoryId && _parentCategoryId != -1)
{
var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id).ToList();
allArticles.AddRange(categoryArticles);
}
}
}
}
}
else
{
allArticles = _allArticlesForPurchase;
}
return allArticles;
}
private async Task FilterItems()
{
await Application.Current.MainPage.Navigation.PushAsync(new ArticlesForPurchaseFiltered());
}
更新
public void GetUserDecision()
{
List<int> userDecision = new List<int>();
if (FilterAllItems) userDecision.Add(_parentCategoryId = -1);
if (FilterBeginnerItems) userDecision.Add(_parentCategoryId = 1);
if (FilterIntermediateItems) userDecision.Add(_parentCategoryId = 2);
if (FilterUpperIntermediateItems) userDecision.Add(_parentCategoryId = 3);
if (FilterAdvancedItems) userDecision.Add(_parentCategoryId = 4);
UserDecisionResult = userDecision;
}
private static List<Article> FindAllArticlesForPurchase(List<Article> allArticles)
{
var result = UserDecisionResult;
if (_parentCategoryId != -1)
{
foreach (var categoryGroup in _allUserCategoryGroups)
{
var allGroupCategories = _allCategories.Where(m => m.CategoryGroupId == categoryGroup.Id).ToList();
if (_parentCategoryId != -1)
{
foreach (var category in allGroupCategories)
{
if (category.ParentId == _parentCategoryId && _parentCategoryId != -1)
{
//var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id).ToList();
var categoryArticles = _allArticlesForPurchase.Where(m => m.CategoryId == category.Id && result.Contains(m.CategoryId)).ToList();
allArticles.AddRange(categoryArticles);
}
}
}
}
}
else
{
allArticles = _allArticlesForPurchase;
}
return allArticles;
}
解决方案
您可以使用 LINQ 按多项选择进行过滤
// these are the ids selected by the user
var list = new List<int>() { 1, 2, 3 };
// this will return all articles whose CategoryID is in list
var result = FilterArticlesForPurchase.Where(list.Contains(x.ArticleCategoryId).ToList();
推荐阅读
- c - 无法存储 libcurl 结果。(输出空文件或不写入内存)
- android-emulator - 从另一台机器进行 RDP 时,Android 模拟器在主机上崩溃
- flutter - 如何使用 dart 从 android 设备中获取 .mp3 文件?
- android - 调试时找不到局部变量
- r - R flextable 如何在新的浏览器选项卡中打开超链接
- iphone - Xcode - 如何获得 iPhone 11 模拟器进行开发?
- c++ - 从朋友类继承时无法使用大括号括起来的初始化列表
- java - 椭圆曲线资源
- react-native - 从 React-Native 中的数据文件中获取 uri props
- webcam - 如何在 Windows Media Foundation 中使用“H.264 UVC 1.5 相机编码器”