Xamarin.Forms update Entry value from Xamarin.Essentials.Contacts selection using MVVM

Viewed 133

I'm trying to update the value of an Entry control using Xamarin.Essentials.Contacts.

I have a PhoneNumber Entry that's empty when opening the page but there's a button below it that says 'Select Contact' and I'm retrieving the phone number and am trying to set the value of the PhoneNumber Entry to that phone number.

I can't seem to be able to do it and I don't really know how to do it.

When I select the contact I get the number but it doesn't update the Entry value.

Here's my code

XAML

<StackLayout Orientation="Horizontal"
                                 Padding="16,12"
                                 Spacing="12">
                    <Frame HorizontalOptions="FillAndExpand"
                               Padding="6, 1"
                               Margin="5, 0"
                               CornerRadius="8"
                               BackgroundColor="#212121">
                        <Entry x:Name="PhoneNumberEntry"
                               Text="{Binding PhoneNumberEntry, Mode=TwoWay}"
                               Placeholder="Don't add '+1'" 
                               Keyboard="Numeric"
                               MaxLength="10"
                               Style="{StaticResource EntryStyle}" />
                    </Frame>
                </StackLayout>

                <StackLayout Orientation="Horizontal"
                                 Padding="16,12"
                                 Spacing="12">
                    <Button x:Name="ContactButton"
                               Text="Or select contact"
                                Command="{Binding SelectContactCommand}"
                            BackgroundColor="Red"
                            HorizontalOptions="FillAndExpand" />
                </StackLayout>

ViewModel

public class InviteViewModel : BaseViewModel
{
    public InviteViewModel()
    {
        SelectContactCommand = new Command(async () => await ExecuteSelectContactCommand());
        Info = new StringBuilder();
    }

    public string PhoneNumberEntry { get; set; }
    public Command SelectContactCommand { get; set; }
    public StringBuilder Info { get; set; }

    private async Task ExecuteSelectContactCommand()
    {
        try
        {
            var contact = await Contacts.PickContactAsync();
            if (contact == null)
            {
                return;
            }

            Info.AppendLine(contact.Phones.FirstOrDefault()?.PhoneNumber ?? string.Empty);
            var phoneNumber = Info.ToString().Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").TrimEnd();
            PhoneNumberEntry = phoneNumber;
        }
        catch (Exception ex)
        {
        }
    }
}
1 Answers

Like @Jason and @AppPack said, it was only a matter of using OnPropertyChanged() in the setter of PhoneNumberEntry

private string phoneNumberEntry;

    public string PhoneNumberEntry
    {
        get { return phoneNumberEntry; }
        set
        {
            phoneNumberEntry = value;
            OnPropertyChanged("PhoneNumberEntry");
        }
    }
Related