简体   繁体   中英

How to get entry backbutton press event in behaviors in xamarin forms

I have an entry for entering the mobile number, I want back button press event when erasing the entered mobile number in behaviours.

I have tried OnBackButtonPressed() in view code behind. But it is not fired when erasing the entry text. I have referred many sites but I didn't get any clear solution. Please give your valuable suggestion.

在此处输入图片说明

<Entry x:Name="phoneEntry" Placeholder="Phone Number" FontSize="14" PlaceholderColor="Gray" Text="{Binding Number}" HorizontalOptions="FillAndExpand" Keyboard="Telephone">
      <Entry.Behaviors>  
          <behavior:EntryBehavior CommandParameter="{x:Reference phoneFormat}"/>                                                            
      </Entry.Behaviors>
</Entry>

PhoneNumber Format Behavior

 public class EntryBehavior : Behavior<Entry>
{
    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    public static readonly BindableProperty CommandParameterProperty =
        BindableProperty.Create("CommandParameter", typeof(object), typeof(EntryBehavior));        

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.TextChanged += Bindable_TextChanged;
    }

    private void Bindable_TextChanged(object sender, TextChangedEventArgs args)
    {
        if (CommandParameter != null)
        {
            var index = (CommandParameter as MyList).SelectedIndex;

            if (index == 0 && ((Entry)sender).Text.Length < 14)
            {
                var value = args.NewTextValue;

                if (string.IsNullOrEmpty(args.OldTextValue) && !args.NewTextValue.Contains("("))
                {
                    ((Entry)sender).Text = "(" + value;
                    return;
                }

                if (value.Length == 4)
                {
                    ((Entry)sender).Text += ") ";
                    return;
                }

                if (value.Length == 9)
                {
                    ((Entry)sender).Text += "-";
                    return;
                }
            }
            if (index == 1 && ((Entry)sender).Text.Length < 14)
            {
                var value = args.NewTextValue;
                if (((Entry)sender).Text.Length == 1 && !((Entry)sender).Text.Contains("("))
                {
                    ((Entry)sender).Text += "(";
                    return;
                }

                if (value.Length == 5)
                {
                    ((Entry)sender).Text += ") ";
                    return;
                }

                if (value.Length == 7 && !((Entry)sender).Text.Contains(" ") && !((Entry)sender).Text.Contains(")"))
                {
                    ((Entry)sender).Text += " ";
                    return;
                }

                if (value.Length == 10)
                {
                    ((Entry)sender).Text += "-";
                    return;
                }
            }

            ((Entry)sender).Text = args.NewTextValue;
        }
    }       

    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);
    }
}

I want to format the phone number based on type

Type1 : (xxx) xxx-xxxx

Type2 : x(xxx) xxx-xxxx

First solution
Assuming you use bindings
<Entry Text="{Binding EntryText}" HorizontalOptions="Center"/>
in your ViewModel change EntryText implementation to

private string entryText;
public string EntryText
{
    get => entryText;
    set
    {
        if (!string.IsNullOrEmpty(entryText) && value.Length < entryText.Length)
        {
            Console.WriteLine("Char was deleted");
        }

        SetProperty(ref entryText, value);
    }
}

Second solution
You don't use MVVM pattern and you do everything in code behind.Change you entry line to
<Entry Grid.Row="2" TextChanged="Handle_TextChanged" HorizontalOptions="Center"/>

Now, create a Handle_TextChanged implementation:

public void Handle_TextChanged(object sender, TextChangedEventArgs e)
{
    if (!(sender is Entry label))
    {
        return;
    }

    if (!string.IsNullOrEmpty(e.OldTextValue) && e.NewTextValue.Length < e.OldTextValue.Length)
    {
        Console.WriteLine("Char deleted");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM