locked
Passing and consuming data passed into a Viewmodel while navigating (InitializeAsync) RRS feed

  • Question

  • User389101 posted

    Good day, I am trying to consume data passed into the InitializeAsync method of a view model but i does not work, I have it like this:

    namespace StudentData
        {
        public class StudentOverViewViewModel : ViewModelBase
        {
        private DataSet studentData;
        public Icommand getDetails 
        {
            get;
            set;
        }
        public DataSet _data
        {
            get
             { return studentData;
    
             }
                  set
                    {
                     studentData= value;
                    RaisePropertyChanged(() => studentData);
                 }
        }
    
    
        public StudentOverViewViewModel (DataSet studentData)
        {
            this.studentData = studentData;
            getDetails  = new Command(Details);
        }
    
        public async Task getDetails()
        {
            // api calls done to retieve data and set studentData to the current student data
            await NavigationService.NavigateToAsync<StudentDetailViewModel>(studentData );
        }
    }
    
    }
    

    for the second view model :

    namespace StudentData
    
    {
    
        public class StudentDetailViewModel: ViewModelBase
            {
                private DataSet Data;
    
                public DataSet _Data
                {
                    get
                         { return Data;
    
                         }
                        set
                            {
                                Data= value;
                                RaisePropertyChanged(() => Data);
                         }
                }
        public StudentDetailViewModel(DataSet Data)
            {
                this.Data = Data;
            }
    
        public override  async Task InitializeAsync(object navigationData)
            {
            if(navigationData is DataSet)
            {
                Data = (DataSet) navigationData;  // after the page is initialized, the variables or properties/ models are not updated and is still null
            }
        }
    }
    
    }
    

    this does not update the property Data in this view model, Thank you in advance for your help.

    Monday, September 16, 2019 6:51 PM

All replies

  • User369979 posted

    Which plugin are you using? We use Navigation directly to push a new page in Forms.

    Tuesday, September 17, 2019 5:22 AM
  • User389101 posted

    Thanks for the reply, i wrote my own navigation service and i am using viewmodel first navigation

    Tuesday, September 17, 2019 2:13 PM
  • User389101 posted

    My problem is that i pass in my parameter to my next view model and i set the properties in that view model in my initializeAsync method, but after the page is initialized, the properties are then set back to null

    Tuesday, September 17, 2019 3:27 PM
  • User21936 posted

    I think you have some odd naming there. Your public property is _Data (notice the initial underscore) and your private back store field is Data. The convention is the other way around. In any case you are setting the back store private Data field which won't fire the RaisePropertyChanged(() => Data); Perhaps try this instead:

        public class StudentDetailViewModel: ViewModelBase
            {
                private DataSet _data;
    
                public DataSet Data
                {
                    get
                         { return _data;
    
                         }
                        set
                            {
                                _data = value;
                                RaisePropertyChanged(() => Data);
                         }
                }
        public StudentDetailViewModel(DataSet Data)
            {
                this.Data = Data;
            }
    
        public override  async Task InitializeAsync(object navigationData)
            {
            if(navigationData is DataSet)
            {
                Data = (DataSet) navigationData;  // after the page is initialized, the variables or properties/ models are not updated and is still null
            }
        }
    }
    

    Not sure what you should be passing in to the RaisePropertyChanged(() => Data); since I can't see your base view model code, IOW not sure if you need to pass in the public property or the private backing store member variable. Mostly I see people passing in the name of the public property, not a lambda that returns the public property.

    Tuesday, September 17, 2019 7:09 PM