我的Android应用程序的问题很小,我不知道如何使用MVVM Cross解决它.
这是我的viewmodel:
public class AddressesShowviewmodel : Mvxviewmodel{ public List<Address> Addresses { get; set; } public AddressesShowviewmodel(string addressesForListVIEw) { Addresses = JsonConvert.DeserializeObject<List<Address>>(addressesForListVIEw); } public IMvxCommand Showitemcommand { get { //return new MvxRelayCommand<Type>((type) => this.RequestNavigate(type)); return new MvxRelayCommand(DoShowContact); } } private Address selectedItem; public Address SelectedItem { get { return selectedItem; } set { selectedItem = value; FirePropertyChanged(() => SelectedItem); } } private voID DoShowContact() { RequestNavigate<AddressShowviewmodel>(); }}
我的AddressesShow.axml
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:local="http://schemas.androID.com/apk/res/INMobileCRM4AndroID.INMobileCRM4AndroID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" ><Mvx.MvxBindableListVIEw androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" local:MvxBind="{'ItemsSource':{'Path':'Addresses'},'ItemClick':{'Path':'Showitemcommand'}, 'SelectedItem':{'Path':'SelectedItem'}}" local:MvxItemTemplate="@layout/addressListitem" /></FrameLayout>
我想知道,我如何从AddressesShow.axml中的ListVIEw获取SelectedItem ..我试图创建一个Property’SelectedItem’..但它在开始时被调用,当viewmodel被创建时(显然正在返回) null),而不是单击Item时..它的btw是一种Address,而不仅仅是String或者什么……也许有什么建议吗?
解决方法:
Dir的SelectedItem缺乏在上周为DanIEl在Build的演讲做准备时被确定为一个问题.
要解决它,有几个快速的答案:
1您可以使用SelectedItemposition进行绑定 – 这是一个int
2您可以使用Click ICommand / IMvxCommand绑定而不是使用SelectedItem – 在您的示例中,这将是相同的axml但
public IMvxCommand Showitemcommand{ get { return new MvxRelayCommand<Address>(address => DoShowContact(address)); }}
要明确这个上面的点击选项就是我要用的.
如果确实需要SelectedItem ……
然后,为了得到一个完整的答案,DanIEl和我制作了一个新的绑定原型.使用以下方式注册此绑定:
registry.RegisterFactory(new MvxCustomBindingFactory<MvxBindableListVIEw>("SelectedItem", adapterVIEw => new MvxAdapterVIEwSelectedItemTargetBinding(adapterVIEw)));
并包含逻辑:
using System;using AndroID.Widget;using CirrIoUs.MvvmCross.Binding.DroID.VIEws;using CirrIoUs.MvvmCross.Binding.Interfaces;using CirrIoUs.MvvmCross.Interfaces.Platform.Diagnostics;namespace CirrIoUs.MvvmCross.Binding.DroID.Target{#warning This needs to be redone for all adaptervIEws not just List vIEw!#warning The use of ItemClick instead of ItemSelected needs to be reinvestigated here! public class MvxAdapterVIEwSelectedItemTargetBinding : MvxBaseandroidTargetBinding { private Readonly MvxBindableListVIEw _vIEw; private object _currentValue; public MvxAdapterVIEwSelectedItemTargetBinding(MvxBindableListVIEw vIEw) { _vIEw = vIEw; ((ListVIEw)_vIEw).ItemClick += OnItemClick; } private voID OnItemClick(object sender, AdapterVIEw.ItemClickEventArgs itemClickEventArgs) { var container = (_vIEw.GetItemAtposition(itemClickEventArgs.position) as MvxJavaContainer); if (container == null) { MvxBindingTrace.Trace(MvxTraceLevel.Warning, "Missing MvxJavaContainer in MvxAdapterVIEwSelectedItemTargetBinding"); return; } var newValue = container.Object; if (!newValue.Equals(_currentValue)) { _currentValue = newValue; FireValueChanged(newValue); } } public overrIDe voID SetValue(object value) {#warning Sort out Equals test here if (value != null && value != _currentValue) { var index = _vIEw.Adapter.Getposition(value); if (index < 0) { MvxBindingTrace.Trace(MvxTraceLevel.Warning, "Value not found for spinner {0}", value.ToString()); return; } _currentValue = value; _vIEw.SetSelection(index); } } public overrIDe MvxBindingMode DefaultMode { get { return MvxBindingMode.TwoWay; } } public overrIDe Type targettype { get { return typeof(object); } } protected overrIDe voID dispose(bool isdisposing) { if (isdisposing) { ((ListVIEw)_vIEw).ItemClick -= OnItemClick; } base.dispose(isdisposing); } }}
为了测试这个工作,我使用Tutorial PullToRefresh代码改编使用:
<Mvx.MvxBindableListVIEw androID:ID="@androID:ID/List" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" local:MvxBind="{'ItemsSource':{'Path':'Emails'},'ItemClick':{'Path':'Showitemcommand'},'SelectedItem':{'Path':'TheSelectedEmail'}}" local:MvxItemTemplate="@layout/Listitem_email"/>
和:
public class SimpleEmail { public string From { get; set; } public string header { get; set; } public string Message { get; set; } } private ObservableCollection<SimpleEmail> _emails; public ObservableCollection<SimpleEmail> Emails { get { return _emails; } private set { _emails = value; RaisePropertyChanged(() => Emails); } } private SimpleEmail _email; public SimpleEmail TheSelectedEmail { get { return _email; } set { _email = value; MvxTrace.Trace(MvxTraceLevel.Error, "HELLO {0} ", value == null ? "null" : value.From); } }
在所有这些工作中要注意的一件事是AndroID中的列表视图选定项与Silverlight / wp中的列表框选定项略有不同 – 例如在androID中获取ListvIEw以突出当前选择可能会非常困难,并且很难让ListvIEw生成选择更改事件.
注意:我已将DroID SelectedItem上的问题记录到https://github.com/slodge/MvvmCross/issues/52 – 我将确保在不久的将来将绑定添加到核心库中
总结以上是内存溢出为你收集整理的android – MVVMCross从MvxBindableListView获取SelectedItem全部内容,希望文章能够帮你解决android – MVVMCross从MvxBindableListView获取SelectedItem所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)