Recommended approach to initialize Command

I’ve two commands in my View Model and one of them is initialized using lazy loading and other using eager loading.

Which approach is good to go? Should I use lazy loading or eager for both?

View Model:

public class LoginPageViewModel : MvxViewModel
    public LoginPageViewModel()

    public MvxCommand<object> LoginCommand { get; set; }

    private MvxCommand<object> signUpCommand;
    public MvxCommand<object> SignUpCommand
            return signUpCommand ?? (signUpCommand = new MvxCommand<object>(o => {
                ShowViewModel<SignupPageViewModel> ();

    private void InitializeCommands()
        LoginCommand = new MvxCommand<object>(LoginUser);


public override void ViewDidLoad ()
    base.ViewDidLoad ();

    var set = this.CreateBindingSet<LoginPageView, LoginPageViewModel> ();
    set.Bind (CreateAccountButton).To (vm => vm.SignUpCommand);
    set.Bind (ResetPasswordButton).To (vm => vm.ResetPasswordCommand);

    set.Apply ();

I’m using that within iOS app developed using Xamrian.iOS and MVVMCross


Unless you have a good reason, I would go with the simpler approach, that is, eager loading.

And, assuming you can use C# 6.0, I would try to simplify the code even more:

public MvxCommand<object> LoginCommand { get; } = new MvxCommand<object>(LoginUser);
public MvxCommand<object> SignUpCommand { get; }
    = new MvxCommand<object>(o => ShowViewModel<SignupPageViewModel> ());

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *