ossentoo
ossentoo

Reputation: 2019

Xamarin Forms: WhenActivated not being fired reactiveui for viewmodel

I can't understand why the WhenActivated func is not fired for a Xamarin Forms application. The application has a LoginViewModel and a LoginView

Here's the sample application code.

The first view LoginView is loaded as expected. However, I would ideally like to load services and setup bindings in the WhenActivated func but it is not firing. See the LoginViewModel.

Any ideas?

Source code

thanks

Upvotes: 1

Views: 650

Answers (1)

Adrián Romero
Adrián Romero

Reputation: 620

Ok ossentoo, I'm not sure why but seems like if you want to use WhenActivated on your viewmodel you must use when activated on your view, the bindings are placed on view's code behind. Here is a little example:

public class LoginViewModel : ViewModelBase /*IRoutableViewModel, ISupportsActivation etc*/
{

    this.WhenActivated(disposables =>
        {
            Debug.WriteLine("ViewModel activated.").DisposeWith(disposables);
        });

}

On the view:

public partial class LoginView : ContentPageBase<LoginViewModel>
{
    public LoginView()
    {
        InitializeComponent ();
        this.WhenActivated(new Action<CompositeDisposable>(c =>
        {
            System.Diagnostics.Debug.WriteLine("From View ! (When Activated)");

          /*instead of xaml bindings you can use ReactiveUI bindings*/
           this.Bind(ViewModel, vm => vm.UserName,
                v => v.UserName.Text).DisposeWith(disposables);
            this.Bind(ViewModel, vm => vm.Password,
                v => v.Password.Text).DisposeWith(disposables);
        }));
    }
}

Let me know if this helped you. :)

Upvotes: 3

Related Questions