INotifyPropertyChanged does not update View.

TrixoTrixo Member ✭✭

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 11

    @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.

Sign In or Register to comment.