ViewModelActivator does not work in XamForms
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
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>...
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?
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();
}
Actually it does work now without changing anything
@escamoteur is this still an issue? If not, could you please close.
not for me, thanks
Also, make sure you are spelling it correctly. I accidentally spelled it: this.WhenAcitvated