I have a Mac desktop app in Xamarin.
There is a KVO Model that the UI is bound to using interface builder.
I also listen out for changes to the values in the model, and when changed, update attached hardware with the new value over a Serial Port connection.
The problem I have is with the Sliders. The bound value on the slider updates on every step of the slider, rather than when the user releases the slider. This I want so I can update the text field showing the value, but I DON'T want it to trigger my handler until they release.
I can set the slider to continuous = false
, which solves the handler issue, but then the textfield only updates on release.
I can't see anyway to configure the binding to trigger once the user finishes sliding, or a way to throttle the bound callback, so it only runs once within a given timeframe.
- Can you trigger the binding update once the user releases the mouse?
- Is there a way to throttle the callback?
- Would it be better practice to listen out for UI events to update the hardware? Rather than the bound model value changes?
Below is my current set up.....
In the Settings Model
[Register("SettingsModel")]
public class CameraSettingsModel : NSObject
{
Export("GainMode")]
public int GainMode { get; set; }
}
Interface builder settings
In my view controller...
Manually make the var holding the Settings
model KVO compliant as well. (the Settings
model gets swapped out for different models depending on user settings, so needs to trigger a WillChange etc for the bindings to refresh)
private CameraSettingsModel _settings;
[Export("Settings")]
public CameraSettingsModel Settings
{
get
{
return _settings;
}
set
{
WillChangeValue("Settings");
_settings = value;
DidChangeValue("Settings");
}
}
Listen out for changes
Settings.AddObserver("GainValue", NSKeyValueObservingOptions.New, updateGainValue);
Update the hardware
private void updateGainValue(NSObservedChange obj)
{
//update the hardware here
}