Xamarin Slider最小化UWP的限制值

时间:2021-08-14 16:04:39

I noticed an undesired behaviour at Xamarins Slider at UWP if I use the Minimum property. Let's say

如果我使用Minimum属性,我注意到UWP的Xamarins Slider出现了不良行为。比方说吧

<Slider Value="{Binding SliderValue}" Maximum="20" Minimum="10"/>

is bound to

一定会

public double SliderValue { get; set; } = 15;

This means the initial position of the slider should be exactly in the middle. But: During startup, after the Slider got the value (15) it sets the value back to the minimum (10). The slider is now at the minimum left position. Using Minimum="0" the slider is not setting its value back and the initial position is like expected. I also tried to change the order of the slider properties at my XAML but the result is still the same.

这意味着滑块的初始位置应该恰好位于中间。但是:在启动期间,在Slider获得值(15)之后,它将值设置回最小值(10)。滑块现在位于最小左侧位置。使用Minimum =“0”时,滑块不会将其值设置回来,初始位置与预期的一样。我也尝试在我的XAML上更改滑块属性的顺序,但结果仍然相同。

The problem does not occur if I run the app in Android. Android is not setting the bound value back to the minimum.

如果我在Android中运行应用程序,则不会出现此问题。 Android未将绑定值设置回最小值。

Has anybody an idea what could be the problem and / or how to fix it?

有谁知道可能是什么问题和/或如何解决它?

1 个解决方案

#1


0  

Solution 2: If your Max and Min values are constant then Solution 1 is too complicated. I would suggest just binding all three values:

解决方案2:如果您的最大值和最小值保持不变,则解决方案1过于复杂。我建议只绑定所有三个值:

<Slider Value="{Binding SliderValue, Mode = TwoWay}" 
Maximum="{Binding Maximum, Mode = OneWay}" 
Minimum="{Binding Minimum, Mode = OneWay}"/>

And then when you intialize ViewModel insure to set those values in a proper order (it might be required to do this when you initialize the view in order to work properly):

然后,当您初始化ViewModel时,请确保以正确的顺序设置这些值(在初始化视图时可能需要执行此操作才能正常工作):

Minimum = 10;
Maximum = 20;
SliderValue = 15;

Solution 1: Maybe there are better answers, but I can only suggest to use TwoWay binding:

解决方案1:也许有更好的答案,但我只能建议使用TwoWay绑定:

<Slider Value="{Binding SliderValue, Mode = TwoWay}" Maximum="20" Minimum="10"/>

Then in the ViewModel you handle situation like:

然后在ViewModel中处理如下情况:

private double sliderValue = 15;
public double SliderValue
{
    get
    {
        return sliderValue;
    }
    set
    {
        if (logicToCheckIfValueMakesSense())
        {
            sliderValue = value;
        }
        onPropertyChanged(nameof(SliderValue));
    }
}

#1


0  

Solution 2: If your Max and Min values are constant then Solution 1 is too complicated. I would suggest just binding all three values:

解决方案2:如果您的最大值和最小值保持不变,则解决方案1过于复杂。我建议只绑定所有三个值:

<Slider Value="{Binding SliderValue, Mode = TwoWay}" 
Maximum="{Binding Maximum, Mode = OneWay}" 
Minimum="{Binding Minimum, Mode = OneWay}"/>

And then when you intialize ViewModel insure to set those values in a proper order (it might be required to do this when you initialize the view in order to work properly):

然后,当您初始化ViewModel时,请确保以正确的顺序设置这些值(在初始化视图时可能需要执行此操作才能正常工作):

Minimum = 10;
Maximum = 20;
SliderValue = 15;

Solution 1: Maybe there are better answers, but I can only suggest to use TwoWay binding:

解决方案1:也许有更好的答案,但我只能建议使用TwoWay绑定:

<Slider Value="{Binding SliderValue, Mode = TwoWay}" Maximum="20" Minimum="10"/>

Then in the ViewModel you handle situation like:

然后在ViewModel中处理如下情况:

private double sliderValue = 15;
public double SliderValue
{
    get
    {
        return sliderValue;
    }
    set
    {
        if (logicToCheckIfValueMakesSense())
        {
            sliderValue = value;
        }
        onPropertyChanged(nameof(SliderValue));
    }
}