I created a Xamarin Component with two BindableProperties.
public static readonly BindableProperty AddToCartCommandProperty = BindableProperty.Create("AddToCartCommand", typeof(ICommand), typeof(ICommand), default(ICommand), BindingMode.TwoWay);
public static readonly BindableProperty AddToCartCommandParameterProperty = BindableProperty.Create("AddToCartCommandParameter", typeof(object), typeof(object), default(object), BindingMode.TwoWay);
public ProductCurve()
{
InitializeComponent();
prezzo_nuovo.SetBinding(Button.CommandProperty, new Binding("AddToCartCommand", source: this));
prezzo_nuovo.SetBinding(Button.CommandParameterProperty, new Binding("AddToCartCommandParameter"));
}
I would like to pass parameters to the Command, so:
<componenti:ProductCurve
x:Name="productElement"
WidthRequest="180"
ShowPromo ="{Binding ArticoloPromozionale}"
AddToCartCommand="{Binding BindingContext.ItemPriceClickCommand, Source={x:Reference home}}"
AddToCartCommandParameter="{Binding }"
Prezzo="{Binding prezzoLordo, StringFormat='€{0:0.00}'}"
PrezzoVecchio="{Binding prezzoOriginaleL, StringFormat='€{0:0.00}'}"
DescrizioneProdotto="{Binding descart}"
Immagine="{Binding Path=MiniImage}"/>
In the linked ViewModel, i have subscribed the Command
public Command ItemPriceClickCommand
{
get
{
return this.itemPriceCommand ?? (this.itemPriceCommand = new Command(this.ItemPriceClick));
}
}
but it's doesn't work. There is a way to send object in ItemPriceClick(object attached)?
This is the method ItemPriceClick:
private async void ItemPriceClick(object attachedObject)
{
try
{
// Differenziare il tipo di click
if (attachedObject is ArticoloCatalogo articolo)
{
await CarrelloManager.Instance.AddCarrello(articolo);
}
this.NotifyPropertyChanged(nameof(CartItemCount));
}
catch (Exception e)
{}
}
For now, the attachedObject is always null.
question from:
https://stackoverflow.com/questions/66066662/custom-component-xamarin-binding-command-with-parameter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…