Forum Xamarin.Forms

INotifyPropertyChanged does not update View.

Hello everybody,

I have been sitting on this problem for a while. I have no clue whats going on.
I have previously implemented INotifyPropertyChanged with success and It works in the project on multiple places.

Right now I have to display icons based on how high int value is.

Everything below this is INotifyPropertyChanged events and It just changes the bool values to manipulate what to show on view.
My start position is with the one icon. When I click on this icon The Window that was hidden with bool value is vissible and i can increase or decrease the number. I click Save button and back button.
This should display the same Start position but right now It should have different number of icons based on number that I selected in the window.

Its just bool variables that had value adjusted to true and INotify event was fired but the rerender of the binded properties just wont happen.

After that I change to different View, I change back to View with icons and everything works, same code I tried to debug it there is no error, everything is being executed.

Any idea ?

public class SettingIconsVisibility : INotifyPropertyChanged
{
    public bool IconConnectionsVisibility { get; set; }
    public bool IconEraseDeviceVisibility { get; set; }
    public bool IconFirmwareUpdateVisibility { get; set; }
    public bool IconInformationOverviewVisibility { get; set; }
    public bool IconVerifySetupVisibility { get; set; }
    public bool IconCheckLogsVisibility { get; set; }



    public SettingIconsVisibility()
    {

        IconConnectionsVisibility = true;
        IconEraseDeviceVisibility = false;
        IconFirmwareUpdateVisibility = false;
        IconInformationOverviewVisibility = false;
        IconVerifySetupVisibility = false;
        IconCheckLogsVisibility = false;
    }

    public void ShowConnectionMenu()
    {
        this.IconConnectionsVisibility = true;
        this.IconEraseDeviceVisibility = false;
        this.IconFirmwareUpdateVisibility = false;
        this.IconInformationOverviewVisibility = false;
        this.IconVerifySetupVisibility = false;
        this.IconCheckLogsVisibility = false;
        this.UpdateUI();
    }

    public void ShowSingleDeviceMenu()
    {
        this.IconConnectionsVisibility = true;
        this.IconEraseDeviceVisibility = true;
        this.IconFirmwareUpdateVisibility = true;
        this.IconInformationOverviewVisibility = true;
        this.IconVerifySetupVisibility = true;
        this.IconCheckLogsVisibility = true;

        this.UpdateUI();
    }
    public void ShowMultipleDevicesMenu()
    {

        this.IconConnectionsVisibility = true;
        this.IconEraseDeviceVisibility = true;
        this.IconFirmwareUpdateVisibility = true;
        this.IconInformationOverviewVisibility = false;
        this.IconVerifySetupVisibility = false;
        this.IconCheckLogsVisibility = false;

        this.UpdateUI();
    }


    // update UI
    public void UpdateUI()
    {

        onPropertyChanged(nameof(IconConnectionsVisibility));
        onPropertyChanged(nameof(IconEraseDeviceVisibility));
        onPropertyChanged(nameof(IconFirmwareUpdateVisibility));
        onPropertyChanged(nameof(IconInformationOverviewVisibility));
        onPropertyChanged(nameof(IconVerifySetupVisibility));
        onPropertyChanged(nameof(IconCheckLogsVisibility));
    }


    public event PropertyChangedEventHandler PropertyChanged;
    void onPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

Posts

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 2019

    @Trixo

    Your question is a tad ambiguous. However, if I'm interpreting it correctly, I would advise re-factoring your code, so that:

    (1) Your ViewModel class exposes the int property, with a getter and setter, with the setter raising PropertyChanged as appropriate.
    (2) Your ViewModel class does not expose any of the bool properties that it currently does. Your ViewModel should not know about View-specific details, such as whether something is visible.
    (3) You create a ValueConverter (see https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters ) that accepts an integer value as a parameter. The ValueConverter receives the int value from the ViewModel, and checks if it is greater than or equal to the integer parameter. If it is, the ValueConverter returns true, otherwise false.
    (4) You bind the IsVisible property of each of your Images to the int property of the ViewModel, using the ValueConverter with an appropriate integer parameter.

  • TrixoTrixo Member ✭✭

    Ye, It is its hard to explain even. Thanks for you suggestion but I think my logic is coded corectly, There is just something that doesnt connect

    I thought this morning that It could that as I change different windows and I update as well values in the windows IT bugs and wont connect. So I tried to invoke propertychange just after the first changes to layout, Did not work anyway. I just went with changin the view on back button to the same view and It works now. It is just little bit slower.

  • JohnHardmanJohnHardman GBUniversity mod

    @Trixo said:
    I just went with changin the view on back button to the same view and It works now. It is just little bit slower.

    I did that when I very first started using Xamarin.Forms . I don't do it any more . Once you are more comfortable with data binding and MVVM, you'll want to refactor your code to use that instead.

  • TrixoTrixo Member ✭✭
    edited September 2019

    I found the mistake. So basically My menu Is constructed in following way.

    I have different windows

    MainSettingsWindow-> where the icons are displayed. Based on what icon you click on this menu different menu is displayed by hiding Main settings Window using binding and showing different one.

    So It seems that MainSettingsWindow has to be vissible to be able re-render content in it.

    I thought about it but problem was that I Notified change first before the window was vissible and then I notified change after the window was visible. Problem was that The second time I notified the change, There was no change therefore there were no update for my icons.

    So mistake on my side

Sign In or Register to comment.