question

OkanYILMAZ-5788 avatar image
0 Votes"
OkanYILMAZ-5788 asked OkanYILMAZ-5788 commented

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

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

https://docs.microsoft.com/answers/storage/attachments/147453-testcommandparameter.png


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


dotnet-xamarin
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.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered OkanYILMAZ-5788 commented

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.


· 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.

Thank u very much

0 Votes 0 ·