C# 在MvvmCross Xamarin android中使用事件处理程序在视图中显示toast

C# 在MvvmCross Xamarin android中使用事件处理程序在视图中显示toast,c#,xamarin,xamarin.android,mvvmcross,C#,Xamarin,Xamarin.android,Mvvmcross,我拥有的:我有一个RecyclerView列表视图和一个onClick功能 我正在尝试的内容:尝试使用事件处理程序在视图中显示祝酒词 MonkeysViewModel.cs namespace ListView.Core.ViewModels { public class MonkeysViewModel : MvxViewModel { public MvxObservableCollection<Monkey> Monkeys = new Mvx

我拥有的:我有一个RecyclerView列表视图和一个onClick功能

我正在尝试的内容:尝试使用事件处理程序在视图中显示祝酒词


MonkeysViewModel.cs

namespace ListView.Core.ViewModels
{
    public class MonkeysViewModel : MvxViewModel
    {
        public MvxObservableCollection<Monkey> Monkeys = new MvxObservableCollection<Monkey>();
        private MvxCommand<Monkey> _clickFunction;

        public MonkeysViewModel()
        {
            Monkey m1 = new Monkey
            {
                Name = "Baboon",
                Location = "Africa & Asia",
                Details = "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
                Image = "http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
            };

            Monkeys.Add(m1);

            Monkey m2 = new Monkey
            {
                Name = "Capuchin Monkey",
                Location = "Central & South America",
                Details = "The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus.",
                Image = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg"
            };
            Monkeys.Add(m2);
            MonkeyList = Monkeys;

        }

        public MvxObservableCollection<Monkey> MonkeyList
        {
            get { return Monkeys; }
            set { Monkeys = value; RaisePropertyChanged(() => MonkeyList); }
        }


        public IMvxCommand clickFunction
        {
            get { return _clickFunction ?? (_clickFunction = new MvxCommand<Monkey>(OnMemorySelected)); }
        }

        void OnMemorySelected(Monkey selectedMemory)
        {
           //Trying to display a Toast message here using event handler and trigger an event in view so I can show a toast 
        }

    }
}
namespace ListView.Core.Models
{
    public class Monkey
    {
        public string Name { get; set; }
        public string Location { get; set; }
        public string Details { get; set; }
        //URL for our monkey image!
        public string Image { get; set; }
    }
}
namespace ListView.Droid.Views
{
    [Activity(Label = "ListView.Droid", MainLauncher = true, Icon = "@drawable/icon")]
    public class MonkeysView : MvxActivity
    {


        protected override void OnViewModelSet()
        {
            base.OnViewModelSet();
            SetContentView(Resource.Layout.MonkeysView);
        }
    }
}

MonkeysView.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark">

  <MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxItemTemplate="@layout/listitem_monkey"
        local:MvxBind="ItemsSource MonkeyList; ItemClick clickFunction"
        />


 </LinearLayout>

查看模型

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark">

  <MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxItemTemplate="@layout/listitem_monkey"
        local:MvxBind="ItemsSource MonkeyList; ItemClick clickFunction"
        />


 </LinearLayout>
您可以创建自定义事件参数

public class SelectedMonkeyEventArgs : EventArgs
{
    public SelectedMonkeyEventArgs(Monkey  monkey)
    {
        Monkey = monkey;
    }
    public Monkey Monkey { get; }
}
将事件处理程序添加到视图模型中

public event EventHandler<SelectedMonkeyEventArgs> OnMonkeySelected;
查看

订阅/取消订阅活动中的活动

protected override void OnPause()
{
    ViewModel.OnMonkeySelected -= ViewModel_OnMonkeySelected;
    base.OnPause();
}

protected override void OnResume()
{
    ViewModel.OnMonkeySelected += ViewModel_OnMonkeySelected;
    base.OnResume();
}

private void ViewModel_OnMonkeySelected(object sender, GenericWebViewViewModel.SelectedMonkeyEventArgs e)
{
    Toast.MakeText(this, $"Selected Monkey {e.Monkey.Name }", ToastLength.Short)
        .Show();
}

您还可以使用一个插件,该插件允许您从ViewModel创建祝酒词。ACR用户对话框允许跨平台访问来自行动表、警报、确认、祝酒等的不同对话框。