Share via

Another Simple Binding Question

Grime 796 Reputation points
2021-10-14T06:30:00.01+00:00

When I click on a button in xaml, instead of directly opening a new page, I wait for a response from a DisplayPromptAsync to give me a name...

        private async void AddOperatorButton_Clicked(object sender, EventArgs e)
        {
            string newOp = await DisplayPromptAsync("Enter New Operator", "New Operator Name/Callsign (no spaces)");
            if (newOp != null) { 
                // await DisplayAlert("New Operator", "The new Operator is... " + newOp, "OK");
                await Navigation.PushModalAsync(new NewOperatorPage (newOp)
                { 
                  BindingContext = new OperatorModel()
                });
            }
        }

I pass this name to the new page and I want it to populate the "NameEntry" entry of the newly opened xaml page:

                    <Entry
                        x:Name="NameEntry"
                        Text="{ Binding OperatorName}"
                        Placeholder="Enter a name/callsign (no spaces)"
                        PlaceholderColor="LightGray"
                        TextColor="DarkBlue"
                        FontSize="16"
                        Keyboard="Text"
                        Grid.Row="0"
                        Grid.Column="1"
                        Margin="0, 0, 20, 0"/>

How do I do this?

Developer technologies | .NET | Xamarin

Answer accepted by question author

Kyle Wang 5,531 Reputation points Microsoft External Staff
2021-10-14T07:29:57.553+00:00

Hi Grime,

Welcome to our Microsoft Q&A platform!

You should create the constructor for OperatorModel, rather than NewOperatorPage.

The following is the simple demo.

OperatorModel.cs

class OperatorModel  
{  
    public string OperatorName { get; set; }  
  
    public OperatorModel(string ope)  
    {  
        OperatorName = ope;  
    }  
}  

MainPage.xaml.cs

private async void AddOperatorButton_Clicked(object sender, EventArgs e)  
{  
    string newOp = await DisplayPromptAsync("Enter New Operator", "New Operator Name/Callsign (no spaces)");  
    if (newOp != null)  
    {  
        await Navigation.PushModalAsync(new NewOperatorPage()  
        {  
            BindingContext = new OperatorModel(newOp)  
        });  
    }  
}  

Regards,
Kyle


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.