MVVMCROSS Ios绑定ShowViewModel

时间:2014-06-05 08:39:25

标签: ios xamarin mvvmcross

通过调用showviewmodel-method在模型中导航后,我在mvvmcross中遇到数据绑定问题。在android方面,它可以工作。

问题是,导航本身是有效的,但我没有从模型中获取任何数据。

在模型中导航:

ShowViewModel<TeamEventDetailsViewModel>(new { eventID = item.ID });

包含数据的ViewModel:

public class TeamEventDetailsViewModel
        : EventDetailsViewModel
    {
        public TeamEventModel CurrentEvent
        {
            get { return MyCurrentEvent as TeamEventModel; }
            set
            {
                MyCurrentEvent = value; 
                RaisePropertyChanged(() => CurrentEvent);
                TickerModel.Comments = value.Comments;
                RaisePropertyChanged(() => TickerModel);
                LineupModel.Team1Players = value.Team1Players;
                LineupModel.Team2Players = value.Team2Players;
                RaisePropertyChanged(() => LineupModel);
            }
        }

        private EventDetailsLineupViewModel _lineupModel = new EventDetailsLineupViewModel();
        public EventDetailsLineupViewModel LineupModel
        {
            get { return _lineupModel; }
            set { _lineupModel = value; RaisePropertyChanged(() => LineupModel); }
        }

        public TeamEventDetailsViewModel()
        {
          EventToken = MvxMessenger.Subscribe<EventUpdateMessage>(OnEventUpdateMessage);
        }

        private void OnEventUpdateMessage(EventUpdateMessage eventUpdate)
        {
            if (MyCurrentEvent != null && eventUpdate.Event.ID == MyCurrentEvent.ID)
            {
                var updatedEvent = (TeamEventModel)eventUpdate.Event;
                var myEvent = CurrentEvent;
                if(updatedEvent.Score!=null)
                    myEvent.Score = updatedEvent.Score;
                if (updatedEvent.Team1Players != null)
                    myEvent.Team1Players = updatedEvent.Team1Players;
                if (updatedEvent.Team2Players != null)
                    myEvent.Team2Players = updatedEvent.Team2Players;

                CurrentEvent = myEvent;
            }
        }

        protected override void Update(EventModel eventdetails)
        {
            CurrentEvent = (TeamEventModel) eventdetails;
        }

        private string _teststring = "success";

        public string Teststring
        {
            get { return _teststring; }
            set
            {
                _teststring = value;
                RaisePropertyChanged(()=>_teststring);
            }
        }
    }

正如您在底部所看到的,我实现了一个测试字符串来证明功能。

在视图中绑定:

public class TeamEventDetailsView : MvxViewController
    {

        public UILabel TestLabel = new UILabel();

        public TeamEventDetailsViewModel TeamEventDetailsViewModel
        {
            get { return (TeamEventDetailsViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }

        public override void ViewDidLoad()
        {
            View.AddSubview(TestLabel);


            this.CreateBinding(TestLabel).To<TeamEventDetailsViewModel>(vm => vm.Teststring).Apply();

            TestLabel.BackgroundColor = UIColor.Orange;

        }        

        public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();
            TestLabel.Frame=new RectangleF(0,20,View.Frame.Width,80);
        }
    }

所以我再说一遍,导航本身有效但模型中的数据没有显示在视图上。

如果我在View中手动创建ViewModel,那么绑定也可以工作,但在我的情况下我不能这样做,因为数据是根据ViewModel生成的数据来调用导航继续进行的。

手动ViewModel:

TeamEventDetailsViewModel = new TeamEventDetailsViewModel();
TeamEventDetailsViewModel.Init(9816);

正如我所知,我和他在教程中所做的完全相同:

https://www.youtube.com/watch?v=cbdPDZmuHk8

有人对我有建议吗? 感谢。

1 个答案:

答案 0 :(得分:2)

MvvmCross在base.ViewDidLoad()中创建ViewModel - 如果您将该调用添加到ViewDidLoad覆盖,那么一切都应该正常