Forum Xamarin Xamarin.Forms

how to print the input inside a string?

HellooHelloo Member ✭✭

I am trying to print the input entered by the user inside an alert. Can you tell me what I am doing wrong.

void AddButton_Clicked(System.Object sender, System.EventArgs e)
{
if(!string.IsNullOrEmpty( SensorLookup.Text))
{
MessagingCenter.Send<AllocateSensorViewCell, int>(this, MessagingConstants.AllocateSensor, Convert.ToInt32(SensorLookup.Text));
}
else
{
Application.Current.MainPage.DisplayAlert("hello", "Please check the " + SensorLookup.Text + ".", "ok");
}
}


Answers

  • jezhjezh Member, Xamarin Team Xamurai

    When we check the document of Display a prompt,we will find we can use the following code to abtain the input entered by the user inside an alert.

     string result = await DisplayPromptAsync("Title", "message");
    

    If the OK button is tapped, the entered response is returned as a string. If the Cancel button is tapped, null is returned.

    You can find more details about function DisplayPromptAsync in above link;

    • title, of type string, is the title to display in the prompt.
    • message, of type string, is the message to display in the prompt.
    • accept, of type string, is the text for the accept button. This is an optional argument, whose default value is OK.
    • cancel, of type string, is the text for the cancel button. This is an optional argument, whose default value is Cancel.

    And the link above contains a sample, it should be helpful for you.

    A class of the sample(PromptPage.xaml.cs):

       async void OnQuestion1ButtonClicked(object sender, EventArgs e)
            {
                string result = await DisplayPromptAsync("Question 1", "What's your name?", initialValue:string.Empty);
                if (!string.IsNullOrWhiteSpace(result))
                {
                    question1ResultLabel.Text = $"Hello {result}.";
                }
            }
    
            async void OnQuestion2ButtonClicked(object sender, EventArgs e)
            {
                string result = await DisplayPromptAsync("Question 2", "What's 5 + 5?", initialValue:"10", maxLength: 2, keyboard: Keyboard.Numeric);
                if (!string.IsNullOrWhiteSpace(result))
                {
                    int number = Convert.ToInt32(result);
                    question2ResultLabel.Text = number == 10 ? "Correct." : "Incorrect.";
                }
            }
        }
    
  • jezhjezh Member, Xamarin Team Xamurai

    @Helloo
    Hi friend, have you resolved your question?

Sign In or Register to comment.