ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

ViewModelActivator does not work in XamForms

Open escamoteur opened this issue 10 years ago • 7 comments

Do you want to request a feature or report a bug? Bug

What is the current behavior? Instead as descibed in https://docs.reactiveui.net/en/user-guide/when-activated/index.html

the Action passed to this.WhenActivated does not get called on ViewModels in XamForms. It does work on Views.

That's the ViewModel:

namespace PlaygroundXamForms
{
    public class MainPageViewModel : ReactiveObject, IRoutableViewModel, ISupportsActivation
    {

        public string UrlPathSegment { get; }
        public IScreen HostScreen { get; }


        public MainPageViewModel(IScreen screen = null)
        {
            HostScreen = screen ?? Locator.Current.GetService<IScreen>();

            SavedGuid = Guid.NewGuid();


            this.WhenActivated(d => 
            {
                d(NavigateToListView = ReactiveCommand.CreateFromObservable(
                    () => HostScreen.Router.Navigate.Execute(new DemoListViewViewModel(HostScreen))));
            });

        }


        public ReactiveCommand NavigateToListView { get; protected set; }


        Guid savedGuid;
        private readonly ViewModelActivator activator = new ViewModelActivator();

        public Guid SavedGuid {
            get { return savedGuid; }
            set { this.RaiseAndSetIfChanged(ref savedGuid, value); }
        }


        ViewModelActivator ISupportsActivation.Activator
        {
            get { return activator; }
        }
    }
}

You can test it using my PR

https://github.com/reactiveui/ReactiveUI/pull/1168

In the commited Version in MainPageViewModel.cs the this.WenActived is commented out.

**Which versions of ReactiveUI, and which platform / OS are affected by this issue? V7 XamarinForms Android

escamoteur avatar Sep 27 '16 14:09 escamoteur

VM activation definitely works in XF - I use it all the time. Just took a quick squiz at your code and it looks like your main view isn't reactive (i.e. it inherits ContentPage rather than ReactiveContentPage). VM activation only works by virtue of being able to detect activation in the related view, so your view must be activatable. The easiest way to make it activatable is to inherit ReactiveContentPage<TViewModel>...

kentcb avatar Sep 28 '16 00:09 kentcb

I just had a look at ReactiveContentPage, but I don't see a difference to my ViewClass as it implements IViewFor<> too. What does ReactiveContentPage more?

escamoteur avatar Sep 28 '16 06:09 escamoteur

I did this in my custom ContentPage and it looks like it works:

protected override void OnAppearing()
        {
            base.OnAppearing();
            ViewModel.Activator.Activate();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            ViewModel.Activator.Deactivate();
        }

tomnuen avatar Nov 27 '16 15:11 tomnuen

Actually it does work now without changing anything

escamoteur avatar Nov 28 '16 09:11 escamoteur

@escamoteur is this still an issue? If not, could you please close.

kentcb avatar Nov 29 '16 20:11 kentcb

not for me, thanks

escamoteur avatar Nov 29 '16 20:11 escamoteur

Also, make sure you are spelling it correctly. I accidentally spelled it: this.WhenAcitvated

PostImpatica avatar Nov 09 '17 21:11 PostImpatica