【问题标题】:Two ways Binding between a Slider and EntrySlider 和 Entry 之间的两种绑定方式
【发布时间】:2017-08-22 21:33:42
【问题描述】:

我想将条目与滑块绑定,反之亦然。我写了这样的东西:

<Entry x:Name="myEntry" Text="{Binding Value, Mode=TwoWay}"  BindingContext="{x:Reference slider}"/>
<Slider x:Name="slider" Maximum="100" Minimum="0"  BindingContext="{x:Reference myEntry}"/>

当我使用滑块时,条目中的值会更新,但是当我在条目中手动输入一些值时,该值会附加一个 0 或更改为 0。可能是什么问题。我正在开发安卓系统。

【问题讨论】:

  • 您为 Slider 设置了 BindingContext,但实际上没有绑定任何值
  • 其实我猜你不需要给Slider定义BindingContext。一旦你在 Entry as TwoWay 模式下完成它,它应该可以工作
  • 再想一想,您可能希望在两个控件上启用双向绑定,但将它们绑定到 VM 的相同属性。您不会将它们相互绑定
  • @Jason 我的方法错了吗?从理论上讲,它应该按预期工作。我只是不明白我在更改条目的值时遇到的重置为 0

标签: xaml binding xamarin.forms


【解决方案1】:

您应该将 Slider 和 Entry 都绑定到支持视图模型中的字符串/整数。

class MyViewModel 
{
    private int _sliderValue;
    public string EntryText 
    {
        get => _sliderValue.ToString();
        set => SetProperty(ref _sliderValue, int.Parse(value) );
    }

    public int SliderValue
    {
        get => _sliderValue;
        set => (ref _sliderValue, value);
    }
}

在视图中

<Entry Text="{Binding EntryText}" />
<Slider Value="{Binding SliderValue}" />

More MVVM Info

Fresh MVVM for Xamarin

Caliburn Micro for Xamarin

【讨论】:

  • 你的 lambda 表达式给了我很多错误(当前上下文中不存在 SetProperty)。有没有更简单的方法?
  • 它实际上不起作用。我实现了 SetProperty 接口,但它仍然无法正常工作
  • 类需要实现INotifyPropertyChanged
  • 我确实使用了 :PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
【解决方案2】:

请参考以下xaml代码

<Frame HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
        <StackLayout>
        <Entry Text="{Binding Path=Value}"
           FontSize="18"
           x:Name="label"
           BindingContext="{x:Reference Name=slider}"/>
    <Slider x:Name="slider"
            Maximum="1500"
            VerticalOptions="CenterAndExpand" />

        </StackLayout>
    </Frame>

【讨论】:

    【解决方案3】:

    使用视图模型也可以实现,请参考代码

    Xaml

    <Frame HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
            <StackLayout>
                <Entry x:Name="NameEntry" Placeholder="Enter Name"  Text="{Binding Forename,Mode=TwoWay}" />
    
                <Slider Value="{Binding Forename}" Minimum="0"  Maximum="10"/>
    
            </StackLayout>
    
        </Frame>
    

    -------------------- 现在在 c# 文件中查看模型-------------------- ---------------

    public partial class MainPage : ContentPage
    {
    
        public class DetailsViewModel : INotifyPropertyChanged
        {
            int forename;
            public int Forename
            {
                get
                {
                    return forename;
                }
                set
                {
                    if (forename != value)
                    {
                        forename = value;
                        OnPropertyChanged ("Forename");
                    }
                }
    
    
            }
            protected virtual void OnPropertyChanged(string propertyName)
            {
                var changed = PropertyChanged;
                if (changed != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public MainPage()
        {
            InitializeComponent();      
           BindingContext = new DetailsViewModel();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 2015-08-09
      • 2021-10-16
      • 2020-03-12
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多