Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to differentiate changes to an ObservableProperty that come from code vs User Input #920

Open
mkulisic opened this issue Aug 23, 2024 Discussed in #919 · 0 comments

Comments

@mkulisic
Copy link

mkulisic commented Aug 23, 2024

Discussed in #919

Originally posted by mkulisic August 22, 2024
Hello,
I have a boolean ObservableProperty in my application that can be updated either via the UI by the user or the system may update its value automatically. When the property is updated via the UI I need to update the rest of the application about this change. On the other hand, when the system makes the update I only need to update its value in the UI and I don't have to do any processing.

Is there a way to differentiate if the OnPropertyChanged event was triggered by code or by interaction with the UI?

Based on some experimentation I have essentially 2 ways to work around this problem but neither seems very clean:

  1. Set a flag right before doing the system update that prevents the OnPropertyChange code to actually do anything. This doesn't seem clean and I'm not sure if there are any potential complications that could come with this (I'm not very familiar with how threading works in these apps)
  2. Rely on the Events available to each specific control. The issue I'm facing comes because I am attaching my ObservableProperty to a ToggleSwitch. This control doesn't support "OnClick" or sending commands. It only has the "Toggled" event that gets fired regardless of what causes the value of the control to toggle (system vs ui).

Is there another option that I may be missing for how to handle a situation like this? I'd like to know if there is a safe non-control specific solution.

Edit

I think I may have a potential solution:

// Switch test

[ObservableProperty]
private bool _switchTest = false;

// Update from system without triggering OnSwitchTestChanged
public void UpdatePropertyFromSystem(bool value)
{
    if(value != SwitchTest)
    {
        OnPropertyChanging(nameof(SwitchTest));
        _switchTest = value;
        OnPropertyChanged(nameof(SwitchTest));
    }
}

/// <summary>
/// Called with UI changes
/// </summary>
partial void OnSwitchTestChanged(bool value) {
    Console.Write("hi");
}

In this sample I'm emulating the setter but leaving out the part the would call OnSwitchTestChanged. It works but I'm not sure if this is the right way to do this.

Thanks,
Miguel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant