带有 xamarin 绑定到 ListView 的 MVVM Light

MVVM Light with xamarin binding to ListView

我有一个 MVVM Light ViewModel,里面有一个项目:

public ObservableCollection<ObservableKeyValuePair<string, string>> OtherParticipants { get; set; }

项目本身非常简单

[ImplementPropertyChanged]
public class ObservableKeyValuePair<TKey, TValue>
{
    public TKey Key { get; set; }

    public TValue Value { get; set; }
}

我正在尝试将它绑定到我的 Xamarin.Android ListView ww with something like this:

var vm = VmLocator.Chat;
vm.InitClient(client);
vm.InitModel();

var contactsList = FindViewById<ListView>(Resource.Id.lvContacts);

vm.SetBinding(() => vm.OtherParticipants, contactsList.Adapter, BindingMode.TwoWay);

问题是这一行突出显示语法不正确,我绝对确定 contactsList.Adapter 不是我可以将我的集合绑定到它的方式,但正确的方式是什么?此外,如何像在 WPF 中那样定义显示成员。类似于:

<ListBox SelectionMode="Single" ItemsSource="{Binding OtherParticipants}" SelectedItem="{Binding SelectedParticipant}" DisplayMemberPath="Value"/>

我找到了这个问题的有效答案。

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.MainChat);

    var client = Nav.GetAndRemoveParameter<MyClient>(Intent);

    var vm = VmLocator.Chat;
    vm.InitClient(client);
    vm.InitModel();

    var contactsList = FindViewById<ListView>(Resource.Id.lvContacts);

    contactsList.Adapter = vm.OtherParticipants.GetAdapter(ContactsListViewTemplate);
}

private View ContactsListViewTemplate(int position, ObservableKeyValuePair<string, string> participant,
    View convertView)
{
    var view = convertView ?? LayoutInflater.Inflate(Resource.Layout.ParticipantsListItem, null);

    var firstName = view.FindViewById<TextView>(Resource.Id.tvParticipantName);

    firstName.Text = participant.Value;

    return view;
}

其中有价值的部分是 OnCreate 方法的最后两行。我找到了答案 here