Is it possible specify Xamarin Forms Entry Numeric Keyboard without comma or decimal point separator?

后端 未结 4 819
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 20:54

I have to create a form in which the user must input his age. I would like to use a numeric keyboard:

    

        
4条回答
  •  时光取名叫无心
    2021-01-03 21:37

    To me the simple solution is just to add a TextChanged handle to the Entry (this is UI specific code so does not break MVVM)

       
    

    Then in code behind

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            //lets the Entry be empty
            if ( string.IsNullOrEmpty(e.NewTextValue) ) return;
    
            if ( !double.TryParse(e.NewTextValue, out double value) )
            {
                ((Entry) sender).Text = e.OldTextValue;
            }
        }
    

    Change in int.Parse if need an integer

提交回复
热议问题