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

时间:2017-05-10 06:34:14

标签: c# xamarin xamarin.android mvvmcross

我有什么:我有一个RecyclerView ListView和一个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 
        }

    }
}

Monkey.cs

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; }
    }
}

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>

MonkeysView.cs

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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

查看模型

您可以创建自定义EventArgs

public class SelectedMonkeyEventArgs : EventArgs
{
    public SelectedMonkeyEventArgs(Monkey  monkey)
    {
        Monkey = monkey;
    }
    public Monkey Monkey { get; }
}

将事件处理程序添加到View Model

public event EventHandler<SelectedMonkeyEventArgs> OnMonkeySelected;

然后在OnMemorySelected

中调用该事件
void OnMemorySelected(Monkey selectedMemory)
{
   OnMonkeySelected?.Invoke(this, new SelectedMonkeyEventArgs(selectedMemory));
}

查看

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

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();
}