Can parameters be transmitted to a command?

Platform: .NET MAUI| Category: Controls

Yes, you can transmit parameters to a command by using the CommandParameter property. Here is an example involving a parameter:

C#

Button myButton = new Button
 {
     Text = "Click Me",
     Command = new Command((parameter) =>
     {
         string parameterValue = parameter as string;

         if (!string.IsNullOrEmpty(parameterValue))
         {
             DisplayAlert(parameterValue, "Button was clicked!", "OK");
         }
     }),
     CommandParameter = "Hello, .NET MAUI!"
 };

 var stackLayout = new StackLayout
 {
     Children = { myButton }
 };
 Content = stackLayout;

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.