Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to bind and pass from parameters on button command

I have a simple form screen, on click on button I need to pass those from elements to api call. I am not able to pass command parameter as model value . Here is my code.

Xaml page

    <ContentPage.Content>
        <ScrollView>
         <StackLayout Spacing="20" Padding="20" >
               <Label TextColor="#77d065" FontSize = "20" Text="Your Personal Details" />

                <Entry x:Name="FirstName" Placeholder="First Name"
                       Text="{Binding firstname}"/>
                <Entry x:Name="LastName" Placeholder="Last Name"
                       Text="{Binding lastname}"/>
                <Entry x:Name="Email" Placeholder="E-Mail"
                       Text="{Binding email}"/>
                <Entry x:Name="Telephone" Placeholder="Telephone"
                       Text="{Binding telephone}"/>

                <Label TextColor="#77d065" FontSize = "20" Text="Your Address" />


                 <Entry x:Name="Address" Placeholder="Address"
                       Text="{Binding address_1}"/>
                <Entry x:Name="City" Placeholder="City"
                       Text="{Binding city}"/>
                <Entry x:Name="PostCode" Placeholder="PostCode"
                       Text="{Binding postcode}"/>
                <Entry x:Name="Country" Placeholder="Country"
                       Text="{Binding country_id}"/>

                <Entry x:Name="State" Placeholder="State"
                       Text="{Binding State}" />

            <Button x:Name="nextBtn" Text="Next" TextColor="White"
                BackgroundColor="#77D065"
                Command="{Binding NextBtnCommand}" VerticalOptions="End"/>

            </StackLayout>
        </ScrollView>

    </ContentPage.Content>

Xaml Code behind

                    BindingContext = new GuestCheckOutViewModel();

                    GuestUser user = new GuestUser()
                    {
                        firstname = FirstName.Text,
                        lastname = LastName.Text,
                        email = Email.Text,
                        telephone = Telephone.Text,
                        address_1 = Address.Text,
                        city = City.Text,
                        postcode = PostCode.Text,
                        country_id = "99",
                        zone_id = "4231"
                    };


                    nextBtn.CommandParameter = user;

And view model

     public class GuestCheckOutViewModel : INotifyPropertyChanged
        {

            public GuestUser GuestUserResult { get; private set; }
            public ICommand NextBtnCommand { get; private set; }

            public GuestCheckOutViewModel()
            {
                NextBtnCommand = new Command<GuestUser> (GuestCheckOutNext);
            }

            void GuestCheckOutNext(GuestUser guestUser){



                Debug.WriteLine("name {0}", guestUser.firstname);
                Debug.WriteLine("name {0}", guestUser.lastname);
                Debug.WriteLine("name {0}", guestUser.email);
                Debug.WriteLine("name {0}", guestUser.telephone);


                OnPropertyChanged("GuestUserResult");

            }

            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        }

How can I use command and databinding in this simple form page

Best Answers

Answers

Sign In or Register to comment.