How to use Command Parameter with Multiple Component On Xamarin-Mvvm

Okan YILMAZ 26 Reputation points
2021-11-08T15:19:21.41+00:00

Hello, i using Xamarin-Mvvm. i want to send multiple component with CommandParameter. Example;

https://learn-attachment.microsoft.com/api/attachments/147453-testcommandparameter.png?platform=QnA

and i need to get the Text in 2 Editor ( testCustom1, testCustom2 ) how can i do it? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} vote

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,571 Reputation points Microsoft Vendor
    2021-11-09T02:17:24.847+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can use Xamarin.Forms Multi-Bindings with a converter to achieve it.

    I write a simiple code to achieve it with your xaml. I notice you used customEditor and customButton, So I make Editor to Button to test it.

    I suppose that you have Person class.

       public class Person  
           {  
               public string Name { get; set; }  
               public string Age { get; set; }  
           }  
    

    And you want this class as your command parameter. And you want to get the text from the testCustom1 and testCustom2, So I add Binding with <Binding Path="Text" Source="{x:Reference testCustom1}" />, If you want to get other property from customEditor, just use the property name to fill in the Binding Path="property name"

    Your XAML should look like this:

       <ContentPage.Resources>  
               <ResourceDictionary>  
                   <local:PersonConverter x:Key="myPersonConverter" />  
               </ResourceDictionary>  
           </ContentPage.Resources>  
           <ContentPage.Content>  
               <StackLayout>  
                   <Editor  x:Name="testCustom1" />  
                   <Editor  x:Name="testCustom2" />  
         
                   <Button Text="send" Command="{Binding SharePostCommand}">  
                       <Button.CommandParameter>  
                           <MultiBinding Converter="{StaticResource myPersonConverter}">  
                               <MultiBinding.Bindings>  
                                   <Binding Path="Text" Source="{x:Reference testCustom1}" />  
                                   <Binding Path="Text" Source="{x:Reference testCustom2}"/>  
                               </MultiBinding.Bindings>  
                           </MultiBinding>  
                       </Button.CommandParameter>  
                   </Button>  
               </StackLayout>  
           </ContentPage.Content>  
    

    And myPersonConverter is a PersonConverter.cs object.

       public class PersonConverter : IMultiValueConverter  
           {  
               public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
               {  
                   if (values[0]!= null &&values[1]!=null && values.Length == 2)  
                   {  
                       string name = values[0].ToString();  
                       string age = values[1].ToString();  
         
                       return new Person { Name = name, Age = age };  
                   }  
                   return null;  
               }  
         
               public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
               {  
                   throw new NotImplementedException();  
               }  
           }  
    

    In the end,you can use Person object as parameter in your SharePostCommand from viewModel:

       public ICommand SharePostCommand { protected set; get; }  
               public MyViewModel()  
               {  
                   SharePostCommand = new Command<Person>(async (key) => {  
                       Person person = key as Person;  
         
                       if (person != null)  
                       {  
                           Console.WriteLine(person.Name + "===============" + person.Age);  
                       }  
                   });  
         
               }  
    

    Best Regards,

    Leon Lu


    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.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful