XAML 小数第一个价格条目

mfpqipee  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(134)

有没有办法在Xamarin Forms中实现类似PayPal的“小数优先”输入?

我希望屏幕截图是不言自明的。我基本上希望输入的瞬间除以100。对于一个输入,例如3,85,我希望它显示为
0,03
0,38
3,85
在Xamarin Forms中有好的实现方法吗?
预罐

ykejflvf

ykejflvf1#

这可以通过TextChanged事件来实现,下面的代码供大家参考:
XAML:

<Entry                 
    Keyboard="Numeric"
    TextChanged="Entry_TextChanged"
  />

后面的代码:

private void Entry_TextChanged(object sender, TextChangedEventArgs e) 
        {
            

            var entry = (Entry)sender;
            var amt = e.NewTextValue.Replace("$", "");

            if (decimal.TryParse(amt, out decimal num))
            {
                // Init our number
                if (string.IsNullOrEmpty(e.OldTextValue))
                {
                    num = num / 100;
                }

                // Shift decimal to right if added a decimal digit
                else if (num.DecimalDigits() > 2 && !e.IsDeletion())
                {
                    num = num * 10;
                }

                // Shift decimal to left if deleted a decimal digit
                else if (num.DecimalDigits() < 2 && e.IsDeletion())
                {
                    num = num / 10;
                }

                entry.Text = num.ToString("C");
            }

            else
            {
                entry.Text = e.OldTextValue;
            }
        }
    }

    public static class ExtensionMethods
    {
        public static int DecimalDigits(this decimal n)
        {
            return n.ToString(System.Globalization.CultureInfo.InvariantCulture)

                    .SkipWhile(c => c != '.')

                    .Skip(1)

                    .Count();
        }

        public static bool IsDeletion(this TextChangedEventArgs e)
        {
            return !string.IsNullOrEmpty(e.OldTextValue) && e.OldTextValue.Length > e.NewTextValue.Length;
        }

    }

Reference link

7d7tgy0s

7d7tgy0s2#

我一直在考虑您的问题,并得到了一些您可能想要的东西。尝试将您的textBox与属性Text="{Binding InputValue}绑定。然后通过向您的主类键入: INotifyPropertyChanged来实现INotifyPropertyChanged接口。
创建名为
PropertyChanged
public event PropertyChangedEventHandler并添加所需内容:

private void OnPropertyChanged(string propertyName)
{
  PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

不要忘记设置属性,通过键入InputValue = 0.00MaddDataContext = this在下面**InitializeComponent();**也是。
像这样创建完整 prop 非常重要:

private decimal inputValue;

public decimal InputValue
{
  get { return inputValue; }
  set
  {
    inputValue = value;
    OnPropertyChanged("InputValue");
  }
}

现在,您必须将textBox设置为ReadOnly并添加KeyDown事件:

private void InputPriceTextBox_KeyDown(object sender, KeyEventArgs e)
{
  int num = 0;
  string stringVal = e.Key.ToString();

  if (stringVal.Length > 1)
  {
    if (stringVal.Contains("NumPad"))
    {
      num = Convert.ToInt32(stringVal.Substring(6));
    }
    else if (stringVal.Contains("D"))
    {
      num = Convert.ToInt32(stringVal.Substring(1));
    }
    else
    {
      return;
    }

    if (InputValue == 0.00M)
    {
      InputValue = 0.00M + (decimal)num / 100;
    }
    else
    {
      InputValue *= 10;
      InputValue += (decimal)num / 100;
    }
  }
}

对我很有效,你自己试试吧!
这是一个WPF的工作版本,但希望您能将其更改为Xamarin(移动的设备没有数字键盘等)。

相关问题