Post Impatica
Post Impatica

Reputation: 16393

How to use Reactive's WhenActivated in a base Content Page

I'm using a reactive content page base but how do I access something from a base viewmodel? Do I have to use the object of the actual base viewmodel?

I've tried something like this but intellisense is barking at me for obvious reasons. How do I access the a command I added to my base veiwmodel that all my view models extend. The base viewmodel is called "BaseViewModel".

Also, can you have .WhenActivated in your ContentPageBase as well as in your Content Pages or does this mess something up?

 public class ContentPageBase<TViewModel> : ReactiveContentPage<TViewModel>, IContentPageBase where TViewModel : class
{
    protected IBindingTypeConverter bindingDoubleToIntConverter;
    private Subject<Unit> clearMessageQueueSubject;

    public ContentPageBase() : base()
    {
        clearMessageQueueSubject = new Subject<Unit>();
        bindingDoubleToIntConverter = (IBindingTypeConverter)App.Container.Resolve<IDoubleToIntConverter>();

        this
            .WhenActivated(
                disposables =>
                {       
                   // VS complains when I try to use TViewModel as first param                 
                    clearMessageQueueSubject.InvokeCommand(TViewModel, x => x.ClearMessageQueueCommand).DisposeWith(disposables);
                });
    }

Upvotes: 1

Views: 456

Answers (2)

Post Impatica
Post Impatica

Reputation: 16393

based on Diego Rafael Souza's answer I got it working by doing the following:

public class ContentPageBase<TViewModel> : ReactiveContentPage<TViewModel>, IContentPageBase where TViewModel : BaseViewModel
{
    protected IBindingTypeConverter bindingDoubleToIntConverter;
    public Subject<Unit> clearMessageQueueSubject;

    //protected Settings setting;
    public ContentPageBase() : base()
    {
        clearMessageQueueSubject = new Subject<Unit>();
        bindingDoubleToIntConverter = (IBindingTypeConverter)App.Container.Resolve<IDoubleToIntConverter>();


        this
            .WhenActivated(
                disposables =>
                {                        
                    clearMessageQueueSubject.InvokeCommand(this.ViewModel, x => x.ClearMessageQueueCommand).DisposeWith(disposables);
                });
    }

... another option, not necessarily recommended:

public class ContentPageBase<TViewModel> : ReactiveContentPage<TViewModel>, IContentPageBase where TViewModel : class
{
    protected IBindingTypeConverter bindingDoubleToIntConverter;
    public Subject<Unit> clearMessageQueueSubject;

    public ContentPageBase() : base()
    {
        clearMessageQueueSubject = new Subject<Unit>();
        bindingDoubleToIntConverter = (IBindingTypeConverter)App.Container.Resolve<IDoubleToIntConverter>();


        this
            .WhenActivated(
                disposables =>
                {                        
                    clearMessageQueueSubject.InvokeCommand((IBaseViewModel)this.ViewModel, x => x.ClearMessageQueueCommand).DisposeWith(disposables);
                });
    }

Upvotes: 1

Diego Rafael Souza
Diego Rafael Souza

Reputation: 5313

Your constraint must be where TViewModel : BaseViewModel in order to the .Net compiler identify the type correctly.

And maybe you'll need an instance of this type on you Invoke method.

Upvotes: 1

Related Questions