简体   繁体   English

Xamarin.Forms中的多个阶梯状滑块

[英]Multiple stepped sliders in Xamarin.Forms

I have a question about a multiple stepped sliders. 我有一个关于多个阶梯状滑块的问题。 I am trying to create multiple stepped sliders, but the problem is that I don't know how to identify which slider value is changed 我正在尝试创建多个阶梯状滑块,但是问题是我不知道如何确定更改了哪个滑块值

This is my constructor: 这是我的构造函数:

 public FeedbackPage ()
        {
            InitializeComponent ();

            Feedback = new Feedback();

            StepValue = 1.0;

            SliderValueVak.ValueChanged += OnSliderValueChanged;
            SliderValueUitleg.ValueChanged += OnSliderValueChanged;

            this.BindingContext = this;
        }

As you can see if the value of a slider changes, it calls the OnSliderValueChanged method. 如您所见,滑块的值是否更改,它调用OnSliderValueChanged方法。 The code inside this method is shown here below: 下面显示了此方法中的代码:

void OnSliderValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);

            SliderValueVak.Value = newStep * StepValue;
            SliderValueUitleg.Value = newStep * StepValue;
        }

But now if I use one slider, both slider values change, this is because I give them both the same value. 但是现在,如果我使用一个滑块,则两个滑块的值都会改变,这是因为我给它们都赋予了相同的值。 Is there a way to use a if statement or swich case to see which slider value is changed? 有没有一种方法可以使用if语句或夹心大小写来查看更改了哪个滑块值?

sender is the control that fired the event. sender是触发事件的控件。 Simply cast it to the correct type and then you will know which slider was the source 只需将其转换为正确的类型,然后您就会知道哪个滑块是源

void OnSliderValueChanged(object sender, ValueChangedEventArgs e)
    {
        var newStep = Math.Round(e.NewValue / StepValue);

        var slider = (Slider)sender;
        slider.Value = newStep * StepValue;
    }

As jason said, sender is the control that fired the event.But if you want to do some more .You can define a subclass of Slider 就像jason所说的,sender是触发事件的控件,但是如果您想做更多的事情,则可以定义Slider的子类。

class MySlider:Slider
{
    public readonly int tag;
    public MySlider()
    {

    }

    public MySlider(int tag)
    {
        this.tag = tag;
    }

}

And you can set the tag to distinguish multiple stepped sliders. 您可以设置标签以区分多个阶梯状滑块。

SliderValueVak = new MySlider(10);
SliderValueUitleg = new MySlider(20);

Andin the method OnSliderValueChanged 然后在方法OnSliderValueChanged

void OnSliderValueChanged(object sender, ValueChangedEventArgs e)
{
   var newStep = Math.Round(e.NewValue / StepValue);

   MySlider slider = (MySlider)sender;

   if(slider.tag==10)// source is SliderValueVak
    {
      SliderValueVak.Value = newStep * StepValue;
      // do some thing more
    }

   else if(slider.tag == 20) //source is SliderValueUitleg
    {
      SliderValueUitleg.Value = newStep * StepValue;
      // do some thing more
    }

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM