我正在以编程方式创建不同数量的MvxSpinners。 生成的MvxSpinners的数量无法预先确定。 它由用户输入决定。
我有一个List<Beneficiary> 。 每个MvxSpinner都旨在更新集合中的每个Beneficiary 。
由于我无法确定要生成的MvxSpinner的数量(对应于集合中的Beneficiary的数量),因此我不得不使用一个ICommand来处理HandleSelectedItem event所有HandleSelectedItem event 。
挑战
我很难确定要更新的List<Beneficiary>的索引,具体取决于用户单击的MvxSpinner。
一个例子
让
var BeneficiaryList=new List<Beneficiary>()如果集合中有5个Beneficiary对象,则将生成5个MvxSpinner。
如果用户选择了要更新集合索引2的Beneficary ,我如何确定要更新的Beneficary索引?
我试过的
private IList<Beneficiary> _beneficiaryList; public IList<Beneficiary> BeneficiaryList { get { return _beneficiaryList; } set { _beneficiaryList= value; RaisePropertyChanged(() => BeneficiaryList); } } public ICommand UpdateBeneficiary=> new MvxCommand<Beneficiary>(item => { //item is of type Beneficiary //But I do not know which index of BeneficiaryList to update });非常感谢您的帮助。
I am creating varied number of MvxSpinners programmatically. The number of the MvxSpinners generated cannot be predetermined. It is determined by the user input.
I have a List<Beneficiary>. Each MvxSpinner is meant to update each Beneficiary in the collection.
Since I cannot determine the number of MvxSpinner (which corresponds to the count of the Beneficiary in the collection) to be generated, I am forced to have one ICommand to handle all the HandleSelectedItem event of the MvxSpinners.
The Challenge
I am having difficulty determining the index of the List<Beneficiary> to update depending on the MvxSpinner the user clicked.
An Example
let
var BeneficiaryList=new List<Beneficiary>()If there are 5 Beneficiary object in the collection, 5 MvxSpinner will be generated.
If the user selects a MVXSpinner which is meant to update index 2 of the collection, how do i determine the index of Beneficary to update?
What I have tried
private IList<Beneficiary> _beneficiaryList; public IList<Beneficiary> BeneficiaryList { get { return _beneficiaryList; } set { _beneficiaryList= value; RaisePropertyChanged(() => BeneficiaryList); } } public ICommand UpdateBeneficiary=> new MvxCommand<Beneficiary>(item => { //item is of type Beneficiary //But I do not know which index of BeneficiaryList to update });Your help will be deeply appreciated.
最满意答案
您可能还需要一个ICommands列表,每个微调器一个。 在你的视图模型中有这样的东西......
private IList<ICommand> _commands; public IList<ICommand> Commands { get { if (_commands == null) { _commands = BeneficiaryList.Select(x => new MvxCommand<Beneficiary>(item => { ... })); } return _commands; } }并设置这样的绑定(假设你有一个微调器列表)
for (int i = 0; i < spinners.Count; i++) { var spinner = spinners[i]; set.Bind (spinner).To(vm => vm.Commands[i]); }Well, it is interesting to answer my own question.
What I did was to give each Spinner a unique ID that corresponds to the index of the collection.
I created a custom Spinner called MvxSpinnerIndexer extending MvxSpinner (I really do not think it matters. You can just extend Spinner). MvxSpinnerIndexer retrieved the Id and the SelectedItem and then placed the two into a Dictionary
Here is the source for MvxSpinnerIndexer
public class MvxSpinnerIndexer : Spinner { public MvxSpinnerIndexer(Context context, IAttributeSet attrs) : this( context, attrs, new MvxAdapter(context) { SimpleViewLayoutId = global::Android.Resource.Layout.SimpleDropDownItem1Line }) { } public MvxSpinnerIndexer(Context context, IAttributeSet attrs, IMvxAdapter adapter) : base(context, attrs) { var itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId(context, attrs); var dropDownItemTemplateId = MvxAttributeHelpers.ReadDropDownListItemTemplateId(context, attrs); adapter.ItemTemplateId = itemTemplateId; adapter.DropDownItemTemplateId = dropDownItemTemplateId; Adapter = adapter; SetupHandleItemSelected(); } public new IMvxAdapter Adapter { get { return base.Adapter as IMvxAdapter; } set { var existing = Adapter; if (existing == value) return; if (existing != null && value != null) { value.ItemsSource = existing.ItemsSource; value.ItemTemplateId = existing.ItemTemplateId; } base.Adapter = value; } } [MvxSetToNullAfterBinding] public IEnumerable ItemsSource { get { return Adapter.ItemsSource; } set { Adapter.ItemsSource = value; } } public int ItemTemplateId { get { return Adapter.ItemTemplateId; } set { Adapter.ItemTemplateId = value; } } public int DropDownItemTemplateId { get { return Adapter.DropDownItemTemplateId; } set { Adapter.DropDownItemTemplateId = value; } } public ICommand HandleItemSelected { get; set; } public int ViewId { get; set; } private void SetupHandleItemSelected() { ItemSelected += (sender, args) => { //sender. var control = (MvxSpinnerIndexer)sender; var controlId = control.Id; var position = args.Position; HandleSelected(position, controlId); }; } protected virtual void HandleSelected(int position, int? controlId) { var item = Adapter.GetRawItem(position); var content = new Dictionary<string, object> {{"Index", controlId}, {"SelectedItem", item}}; //var param = new ListItemWithIndexModel { Index = controlId, SelectedItem = item }; if (HandleItemSelected == null || item == null || !HandleItemSelected.CanExecute(content)) return; HandleItemSelected.Execute(content); } }In your ViewModel
public ICommand SpinnerSelected => new MvxCommand<Dictionary<string, object>>(item => { var selectedItem = item["SelectedItem"] as ListItemModel;//Cast to the actual model var index = item["Index"] as int?;//The index of the collection to update });I believe this will be useful to the community.
更多推荐
MvxSpinner,Beneficiary,BeneficiaryList,List<Beneficiary>,电脑培训,计算机培训,IT培训&q
发布评论