Behaviors number maximum value, Xamarin Form

Gerson Jose Rodriguez Quintero 51 Reputation points
2021-09-03T22:04:28.927+00:00

good evening. thanks for the help. I need in Xamarin Form, to create a behaivor for an entry that does not allow values greater than 17.

as shown in the picture

for example
17 --------------- 1
28---------------- 2
35---------------- 3

129272-whatsapp-video-2021-09-03-at-44940-pm.gif

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,381 Reputation points Microsoft Vendor
    2021-09-06T07:04:08.923+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!
    When you use the entry behavior like the following code, TextChanged event loops round, the text will always be "1".

    using System;  
    using Xamarin.Forms;  
      
    namespace WorkingWithBehaviors  
    {  
    public class NumericValidationBehavior : Behavior<Entry>  
    {  
    protected override void OnAttachedTo (Entry entry)  
    {  
    entry.Completed += OnEntryTextCompleted;  
                //entry.TextChanged += OnEntryTextChanged;  
                base.OnAttachedTo (entry);  
    }  
    
            protected override void OnDetachingFrom (Entry entry)  
    {  
    entry.Completed -= OnEntryTextCompleted;  
                //entry.TextChanged -= OnEntryTextChanged;  
                base.OnDetachingFrom (entry);  
    }  
    private void OnEntryTextCompleted(object sender, EventArgs e)  
    {  
    double result = double.Parse(((Entry)sender).Text);  
    ((Entry)sender).Text = result > 17 ? (result > 28 ? "3" : "2") : "1";  
    }  
      
    void OnEntryTextChanged (object sender, TextChangedEventArgs args)  
    {  
                double result = double.Parse(args.NewTextValue);  
                ((Entry)sender).Text = result >17? (result > 28?"3": "2") : "1";  
            }  
    }  
    }  
    

    So, you could have a try to use MVVM, ProcessYear method is the key to conver text.

    ViewModel  
    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Text;  
      
    namespace EntryBehaviorDemo  
    {  
        class viewModel: INotifyPropertyChanged  
        {  
            public event PropertyChangedEventHandler PropertyChanged;  
      
            private string year;  
            public string Year { get { return year; } set { if (year != value) { year = ProcessYear(value); OnPropertyChanged(); } } }  
      
            private string ProcessYear(string year_)  
            {  
                if (string.IsNullOrEmpty(year_))  
                    return year_;  
      
                double result = double.Parse(year_);  
                year_ = result > 17 ? (result > 28 ? "3" : "2") : "1";  
      
                return year_;  
            }  
            private void OnPropertyChanged([CallerMemberName] string propertyName = null)  
            {  
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
            }  
        }  
    }  
    

    BindingContext

     public partial class MainPage : ContentPage  
        {  
      
            public MainPage()  
            {  
                InitializeComponent();  
                BindingContext = new viewModel();  
            }  
        }  
    

    XAML

    <StackLayout>  
        <Entry Placeholder="enter number"  Text="{Binding Year, Mode=TwoWay}"></Entry>  
    </StackLayout>  
    

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Gerson Jose Rodriguez Quintero 51 Reputation points
    2021-09-06T21:43:48.27+00:00

    Hello, thank you very much for the answer, it looks interesting but the truth is that it did not work, or rather, I have not been able to implement the code on my app, you could adhere to the code please ???

    thanks

    0 comments No comments