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.