You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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)
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
The text was updated successfully, but these errors were encountered:
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:
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:
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
The text was updated successfully, but these errors were encountered: