question

Grime avatar image
0 Votes"
Grime asked Grime commented

Another Simple Binding Question

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?


dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't understand the question...
DisplayPromptAsync returns a string, that is already your new operator name. Why you have to use a page to enter again the new operator name with an Entry?

0 Votes 0 ·
Grime avatar image Grime alessandrocaliaro ·

The page that is next to open is a form that you fill out that you enter details about the new operator. Their name needs already to have been determined as I want to store the new details in a set of variables starting with the operator's name.

0 Votes 0 ·

1 Answer

KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered Grime commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks heaps. I had a constructor for Operator Model, but not for only one input. Once I created that, it works fine. Thanks again.

0 Votes 0 ·