Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

XForms Slider() with MVVM - How to restrain the value Update?

$
0
0

Hello,

I'm build and App that communicates with some specific hardware. This hardware controls some lights intensity thru dimmering, so I have and slider to set it from 0 to 100.
The problem is: This hardware is very limited and won't stand a flood of notifications, so I wanted to limit the volume of updates in this slider.

So I tried this way:

`
void SetupSlider()
{
m_slider = new Slider()
{
Minimum = 0,
Maximum = 100,
HeightRequest = 70,
};

        m_slider.ValueChanged += OnSliderValueChanged;

        m_slider.SetBinding(Slider.ValueProperty, "DimmerValue", BindingMode.TwoWay);
    }

    void OnSliderValueChanged(object sender, ValueChangedEventArgs e)
    {
        if (!m_timerStarted)
        {
            m_timerStarted = true;
            m_timer = DateTime.Now;
            m_slider.Value = e.NewValue;
        }
        else if (m_timer.AddSeconds(2) < DateTime.Now)
        {
            m_slider.Value = e.NewValue;
            m_timerStarted = false;
        }
    }

`

For some reason, this is working (I used some Debug.WriteLine() to validate it) but I still receiving tons of values at my ViewModel.

So I tried to do the same logic at the ViewModel, and it worked perfectly! But then I got another problem: how do I get the last value inputed by the user, if that value is setted before the 2s timer expires?

I was hoping to use m_slider.Focused += and m_slider.Unfocused +=, but both of them are never called.

To sum it up in a question: How can I restrain the number of values send by the dimmer in certain time and still get the first and last one?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>