Daniel Z.
Daniel Z.

Reputation: 324

MVVMCROSS Ios Binding ShowViewModel

I've got problem with data-binding in mvvmcross after doing the navigation in the model by calling the showviewmodel-method. On the android side it works.

So the Problem is, that the navigation itself is working but I don't get any data from the model.

Navigation in the model:

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

ViewModel which containts the Data:

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

As you can see at the bottom I implemented a teststring to prove functionality.

Binding in the View:

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

So I repeat, the navigation itself works but the data from model doesn't get shown on the view.

If I create the ViewModel manually in the View then the binding works also, but in my Situation I can't do that because the Data is pulled depending on the generated Data from the ViewModel which calls the navigation-proceed.

Manual ViewModel:

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

As I can tell I did exactly the same as Stuard does in his Tutorial:

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

Does anyone has an advice for me? Thanks.

Upvotes: 1

Views: 1001

Answers (1)

Stuart
Stuart

Reputation: 66882

MvvmCross does the ViewModel create in base.ViewDidLoad() - if you add that call to your ViewDidLoad override then everything should work ok

Upvotes: 2

Related Questions