Forum Xamarin Xamarin.Android

I have a question about RelayCommand<T> using MVVM Light

The thing is i have on my MainViewModel this RelayCommand:

public RelayCommand<CalculationInfo> MoveMoney
                return _moneyTransaction
                    ?? (_moneyTransaction = new RelayCommand<CalculationInfo>(
                    transactionInformation =>
                        AmountToShow = (transactionInformation.StandardFee != 0.0M && transactionInformation.Amount > 0.0M) ?
                            (transactionInformation.MoneyIn) ?
                                Calculation.SendMoney(transactionInformation.Amount, transactionInformation.StandardFee, transactionInformation.FixedFee)
                                Calculation.ReceiveMoney(transactionInformation.Amount, transactionInformation.StandardFee, transactionInformation.FixedFee)

I need to bind an EditText.TextChanged event to this relaycommand and send an instance of an object that i have on my MainActivity.cs, until now all i have is this:

Binding amountbinding = this.SetBinding(() => CalculationInformation);
            AmountEditText.SetCommand("TextChanged", Vm.MoveMoney, amountbinding);

Which gives me an InvalidCastException on the SetCommand call, I guess i'm doing something wrong. I just don't know what.

Best Answer


  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @JesuCastillo - You can't bind an event to a command. What you will need is some kind of Event To Command function. MVVMLight has this, however I am not sure with Xamarin Android as I have only used it with WPF and Xamarin Forms.

    Hopefully that leads you in the right direction.

    Worse case you would need to wire up the events yourself.

  • JesuCastilloJesuCastillo VEMember

    I had this doubt because the "Click" event does bind to a RelayCommand without any problem in Xamarin.Android, and does work well... I though I could do the same with TextChanged, I did a non-elegant workaround with a property in the MainViewModel, it's working, I just wanted to do it in a better way.

  • JesuCastilloJesuCastillo VEMember

    That would work for Xamarin.Forms, but I'm working with Xamarin.Android, I will mark that as answer since I didn't specify what I was using exactly, thanks.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭
    edited June 2016

    @JesuCastillo - I am trying to solve similar issue and wanted to ask if you ever find an actual answer to your problem? I have a Toolbar and want to bind MenuItemClick event using MVVMLight. Getting the same error "InvalidCastException" on SetCommand. My project is not Forms. It is Android native with PCL. If you or anyone else can shed more light on this - that would be great.

    P.S. Lack and absolute absence of any documentation is really frustrating...

Sign In or Register to comment.