Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

c# - Custom Component Xamarin Binding Command with Parameter

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...