Fluesopp
Fluesopp

Reputation: 93

How do i detect click event in .Core from view.cs in .Droid using MVVMCross?

I'm currently working on a login screen and I wish for an ImageView to become visible upon "login" button click and rotate with an animation. I know how to animate the object, but since the animation is a unique android feature I have to initiate it from LoginView.cs in the .Droid project with e.g. a function and not simply from LoginViewModel in the .Core project.

I know how to treat MvxCommands in LoginViewModel.cs in .Core (and other MvxBind-ings), but I have no clue how to make the LoginView.cs in .Droid to detect this click event and play the animation from there. I other words, how do I make LoginView.cs in .Droid detect any changes happening in LoginViewModel.cs in .Core?? I have been Googlin' for several hours without any luck. Is this even possible? A solution would be god-sent.

Thanks in advance

Upvotes: 0

Views: 67

Answers (1)

Kiliman
Kiliman

Reputation: 20312

The simplest way to handle ViewModel -> View communication is with a Func/Action delegate. Setup a property in your VM that takes a delegate, then assign the delegate in the view. You can then call the delegate from the VM.

Something like:

public class ViewModel {

    public Action ClickDelegate { get; set; }

    public ICommand ClickCommand {
        get { return new MvxCommand(() => {
                            // call the action method to start animation
                            ClickDelegate?.Invoke();
                         };
        }
    }
}

public class MyView {

    protected override OnCreate()  {
        // register delegate with VM
        ViewModel.ClickDelegate = () => { StartAnimation(); };
    } 

}

Upvotes: 3

Related Questions