【问题标题】:Xamarin forms: How to render same entered A entry text in B entry and vise versaXamarin 表单:如何在 B 条目中呈现相同的输入 A 条目文本,反之亦然
【发布时间】:2017-06-01 18:23:26
【问题描述】:

如何在 B 条目中呈现在 A 条目中输入的文本,反之亦然?

我对 Xamarin 表单开发非常陌生。

ViewModal:下面是 Entry 字段可绑定对象,这里每个字段都有其十进制验证。 要求:如果A条目文本发生变化,B条目文本应该根据输入的文本而变化,反之亦然。

在这里,我在处理 OnPropertyChanged 时遇到了问题。

    private string _inputValues_PercentTimer;
    public string InputValues_PercentTimer
    {
        get { return _inputValues_PercentTimer; }
        set
        {
            _inputValues_PercentTimer = CalculationActions.DecimalValidation(value, _inputValues_PercentTimer, 1, 0.0, 100.0, "");
            OnPropertyChanged("InputValues_PercentTimer");
        }
    }


    private string _inputValues_AppDepth;
    public string InputValues_AppDepth
    {
        get { return _inputValues_AppDepth; }
        set
        {

            _inputValues_AppDepth = CalculationActions.DecimalValidation(value, _inputValues_AppDepth, 3, 0.000, 100.00, "");
            OnPropertyChanged("InputValues_AppDepth");
        }
    }

【问题讨论】:

  • 我在处理十进制验证和 OnPropertyChanged 时遇到问题。
  • 与其说“面临问题”,不如描述一下您遇到的具体问题?
  • 当然,杰森。 A 条目、B 条目文本应根据 A 或 B 中输入的值同时更改,反之亦然。请建议要遵循的步骤,如果您需要更多信息,请告诉我
  • 您有两个属性和两个条目。这两个属性如何相互作用?看起来他们是独立的
  • 此时有独立的,但是当输入A条目文本时,B条目文本应该改变做一些计算,反之亦然。

标签: xamarin xamarin.forms textchanged bindable


【解决方案1】:

对不起英语不好

使用 A 条目与您的视图模型绑定,并在 B 条目中进行交叉引用绑定(我不确定这是正确的术语)。

This link 是关于绑定基础知识的,它可能会对您有所帮助。

下面是一段 XAML 代码: ...

<Slider x:Name="sdrMediumBattery" 
    HorizontalOptions="FillAndExpand" 
    Value="{Binding MediumBattery}"`
    Maximum="100" 
    Margin="0,0,0,10"/>

...

<Label x:Name="lblMediumBattery" 
    BindingContext="{x:Reference sdrMediumBattery}"
    FontSize="Large" 
    HorizontalTextAlignment="Center" 
    WidthRequest="50" 
    Text="{Binding Value, StringFormat='{0:#00}'}"/>

我正在使用“sdrMediumBattery”视图(滑块)中的“值”属性并绑定到标签的“文本”属性。滑块绑定到我的视图模型。

【讨论】:

    【解决方案2】:

    我已经解决了这个问题,防止依赖属性之间的死锁。谢谢大家的支持。

    private string _inputValues_PercentTimer;
    public string InputValues_PercentTimer
    {
        get { return _inputValues_PercentTimer; }
        set
        {
            if (_inputValues_PercentTimer != value && !string.IsNullOrEmpty(value))
            {
                _inputValues_PercentTimer = CalculationActions.DecimalValidation(value, _inputValues_PercentTimer, 1, 0.0, 100.0, "");
                double calRes = 5 + double.Parse(_inputValues_PercentTimer);
                _inputValues_AppDepth = calRes.ToString();
                OnPropertyChanged("InputValues_AppDepth");
                OnPropertyChanged("InputValues_PercentTimer");
            }
        }
    }
    
    private string _inputValues_AppDepth;
    public string InputValues_AppDepth
    {
        get { return _inputValues_AppDepth; }
        set
        {
            if (_inputValues_AppDepth != value && !string.IsNullOrEmpty(value))
            {
                _inputValues_AppDepth = CalculationActions.DecimalValidation(value, _inputValues_AppDepth, 3, 0.000, 100.00, "");
                double calRes = 5 + double.Parse(_inputValues_AppDepth);
                _inputValues_PercentTimer = calRes.ToString();
                OnPropertyChanged("InputValues_PercentTimer");
                OnPropertyChanged("InputValues_AppDepth");
            }
        }
    }
    

    【讨论】:

    • 提示:我建议将“InputValues_PercentTimer”之类的字符串替换为 nameof(InputValues_PercentTimer) 之类的字符串,以避免在未来的重构中意外破坏内容
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多