Can I refresh the Master Content from a ViewModel?

Hello developers,

I am having a problem in my app. In the MasterViewModel I have a menu with some options to navigate inside the app and an image and a label that can be changed by the user in the configuration.
I have implemented the INotifyPropertyChanged interface in all my ViewModels, but I have noticed that the MasterViewModel is only executed once during the application lifetime (the constructor).

So, here is my question... How can I refresh the MasterViewModel from another ViewModel when the user changes some information which is also displayed in the Master?

Thank you so much in advance.

Regards!

Best Answers

  • NMackayNMackay GB mod
    edited April 2017 Accepted Answer

    I usually use messaging to tell the master viewmodel to refresh or change it's state.

    EDIT:

    Sorry, I mis-read the question, I was talking about the detail view model updating the master view model.

  • alexmartinezalexmartinez US ✭✭
    Accepted Answer

    I have implemented in my BaseViewModel a Profile property which is inherited from all ViewModels. When something changes it raises the property change to the viewmodel and updates the master page.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited April 2017

    What do you mean by

    the MasterViewModel is only executed once

    A ViewModel doesn't execute...Not as a whole. Methods in it execute.

    How can I refresh the MasterViewModel from

    Not a good approach. You shouldn't have to micro-manage this.
    A ViewModel is a collection of properties and methods.
    Methods don't have to be refreshed, they're code that runs.
    Properties should be using the same get set concept as anything else. So they don't need to be freshed, you just set values.

    The entire idea of refreshing a ViewModel just doesn't make sense. I can't really picture what you think that is/does/consists of.

  • alexmartinezalexmartinez USMember ✭✭
    edited April 2017

    Ok, let me explain myself:

    I have a MasterViewModel and a ProfileViewModel, both have this two properties:

        private Profiles _profile;
        public Profiles Profile
        {
            get { return MySqlite.GetCurrentProfile(); }
            set
            {
                _profile = value;
                RaisePropertyChanged("Profile");
            }
        }
    

    In the MasterViewModel I display (cannot change values) the image and the name of the user.
    In the ProfileViewModel I display all the properties of Profile's class (the user can change the values).

    Now, the problem is that when I change anything in the ProfileViewModel the changes are not refreshed in the Master.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Well, of course not. Because the Profile property wasn't set/changed so it didn't raise an event. So the UI was never told about a change.

    Can you share the XAML where you are binding to the properties on the profile object?

  • alexmartinezalexmartinez USMember ✭✭

    Here is the MasterView:

        <StackLayout Orientation="Horizontal" BackgroundColor="{StaticResource MenuColor}" HeightRequest="150">
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding UserInfoTapped}" CommandParameter="{Binding .}" />
    </StackLayout.GestureRecognizers>
    <Image Source="{Binding Profile.Image, Mode=TwoWay}" WidthRequest="60" HeightRequest="60" VerticalOptions="Center" Aspect="AspectFill" Margin="0,50,0,0" />
    <Label Text="{Binding Profile.FullName, Mode=TwoWay}" FontSize="16" FontAttributes="Bold" Margin="10,60,0,0" />
    <Label Text="{Binding Profile.Coins, Mode=TwoWay}" FontSize="16" FontAttributes="Bold" Margin="-102,86,0,0" />
    <Image Source="ic_action_home.png" WidthRequest="20" HeightRequest="20" VerticalOptions="Center" Aspect="AspectFill" Margin="0,83,0,0" />
    

    And here is the ProfileView:

    <StackLayout Padding="20" Margin="0, 0, 0, 60">
    <Image Source="{Binding Profile.Image, Mode=TwoWay}" HorizontalOptions="Center" WidthRequest="200" HeightRequest="200">
        <Image.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding ImageCommand}" />
        </Image.GestureRecognizers>
    </Image>
    <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
        <Image Source="ic_action_person.png" />
        <Entry Text="{Binding Profile.FullName, Mode=TwoWay}" Keyboard="Text" WidthRequest="250" />
    </StackLayout>
    <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
        <Image Source="ic_action_phone.png" />
        <Entry Text="{Binding Profile.Phone, Mode=TwoWay}" Keyboard="Telephone" WidthRequest="250" />
    </StackLayout>
    

  • NMackayNMackay GBInsider, University mod
    edited April 2017 Accepted Answer

    I usually use messaging to tell the master viewmodel to refresh or change it's state.

    EDIT:

    Sorry, I mis-read the question, I was talking about the detail view model updating the master view model.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    <Label Text="{Binding Profile.FullName, Mode=TwoWay}"

    This looks as I would expect. I wonder if this is a case of BindingContext being off.
    Do you ever see any data from the profile? Do you see default values?
    What I'm trying to determine is: Does you view ever see the profile, or does it not see changes in the profile.

  • alexmartinezalexmartinez USMember ✭✭

    @ClintStLaurent said:

    <Label Text="{Binding Profile.FullName, Mode=TwoWay}"

    This looks as I would expect. I wonder if this is a case of BindingContext being off.
    Do you ever see any data from the profile? Do you see default values?
    What I'm trying to determine is: Does you view ever see the profile, or does it not see changes in the profile.

    The BindingContext is in the code behind as: BindingContext = new MasterViewModel();
    In the profile I see the data and when is changed it also changes in the view, there is no problem.

    @NMackay said:
    I usually use messaging to tell the master viewmodel to refresh or change it's state.

    EDIT:

    Sorry, I mis-read the question, I was talking about the detail view model updating the master view model.

    I will check it out, thank you.

  • alexmartinezalexmartinez USMember ✭✭
    Accepted Answer

    I have implemented in my BaseViewModel a Profile property which is inherited from all ViewModels. When something changes it raises the property change to the viewmodel and updates the master page.

  • ThiyraashThiyraash Member ✭✭

    @alexmartinez Can you elaborate more on how you did it? I cannot understand how you implement it bro.

Sign In or Register to comment.