如何在MVVMCross中替换ViewModel的View?

时间:2013-08-20 13:19:44

标签: c# android viewmodel mvvmcross

我有以下问题:我有一个ActivityListView,代码非常类似于N = 14,15等,Collect-A-Bull Mvvm Tutorials,以及它当前的ViewModel,ActivityListViewModel

观看代码:

using Android.Views;
using Android.Widget;
using Sfa.Droid.Classes;

namespace Sfa.Droid.Views.Activity
{
    [Activity(Label = "My Activity")]
    public class ActivityListView : MvxActivity
    {
        protected override void OnCreate(Bundle bundle)
        {           
            SetContentView(Resource.layout.ActivityListView);
            base.OnCreate(bundle);

            // Create your application here
        }
    }
}

ViewModel的代码:

using System.Windows.Input;


namespace Sfa.Core.ViewModels
{
    public class ActivityListViewModel : MvxViewModel
    {
        private readonly IActivityService _activityService;
        private readonly MvxSubscriptionToken _collectionChangedToken;

        private Activity _item;

        public Activity Item
        {
            get { return _item; }
            set { _item = value; RaisePropertyChanged(() => Item); }
        }

        public ActivityListViewModel(IActivityService activityService, IMvxMessenger messenger)
        {
            _activityService = activityService;
            ReloadList();
            _collectionChangedToken = messenger.Subscribe<ActivityChangedMessage>(OnActivityChanged);
        }

        private void ReloadList()
        {
            Items = _activityService.All();
        }

        private void OnActivityChanged(ActivityChangedMessage message)
        {
            ReloadList();
        }

        private List<Activity> _items;
        public List<Activity> Items
        {
            get { return _items; }
            set { _items = value; RaisePropertyChanged(() => Items); }
        }

        public ICommand ShowDetailCommand
        {
            get
            {
                return new MvxCommand<Activity>(item => ShowViewModel<ActivityDetailViewModel>(new ActivityDetailViewModel.Nav() { Id = item.Id }));
            }
        }


        private Cirrious.MvvmCross.ViewModels.MvxCommand _addCommand;
        public System.Windows.Input.ICommand AddCommand
        {
            get
            {
                _addCommand = _addCommand ?? new Cirrious.MvvmCross.ViewModels.MvxCommand(DoAdd);
                return _addCommand;
            }
        }

        private void DoAdd()
        {
            ShowViewModel<ActivityAddViewModel>();
        }
    }
}

问题出在这里:我有HomeView,我想显示MvxList(来自ActivityListView)和该布局中的Add Button,而不是ActivityListView.axml,它是如何最初的,但出于某种原因,当我点击添加按钮,它在另一个viewmodel中发送另一个表,另一个viewmodel与这个viewmodel相同,唯一的区别是它使用另一个表和其他字段。有些帮助吗?希望我的问题很明确:(我是初学者,抱歉我的经验不足。

0 个答案:

没有答案