1
votes

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

  • LoginView: inherits from ContentPageBase which itself derives from : ContentPage, IViewFor which I think is expected
  • LoginViewModel: ReactiveObject, IRoutableViewModel, ISupportsActivation

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

1
I just checked your repo and everything looks right, my ViewModelBase is very similar, I think your problema can be related to the fact you're using Autofac instead og Splat, may beAdrián Romero
Thx for your comment. Why would not using Splat be a problem? Can you explain so I check?ossentoo
Adrián, Have a look this my branch github.com/spacelinx/learn-reactiveui/tree/with-splat/src/Learn I've removed autofac now completely, but WhenActivated still isn't being triggered. Any ideas?ossentoo
Let me take a look,Adrián Romero
ossentoo, I just loked your repo, and added a line in your Login.xaml.cs, this.WhenActivated(new Action<CompositeDisposable>(c =>{ /*Some dummy code*/ })); and just worked !Adrián Romero

1 Answers

3
votes

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. :)