Search code examples
c#wpfprismreactiveui

Does ReactiveUI offers PRISM ViewModelLocator like service?


With PRISM framework we do like this:

<UserControl x:Class="ProjectName.MyView"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
</UserControl>

As a result, our DataContext set with instance of MyViewModel.

I tried to find the same technique in ReactiveUI and all I have found is this link. It says that all you need is implement IActivatableViewModel in your viewmodel and if you have WhenActivated block in view code-behind that implement IViewFor<T> view model get activated. 

Questions:

1). What does "get activated" mean? I expect that view model is set by ReactiveUI automatically, but this behavior doesn't occur, so looks like that I misunderstood what "get activated" actually means.
2). Does ReactiveUI provide such service which set view model when view become activated? If yes, could you please provide code samples.


Solution

  • What does "get activated" mean?

    Activation is used to defer the setup of a view model until the corresponing view has been loaded: https://www.reactiveui.net/docs/handbook/when-activated.

    I expect that view model is set by ReactiveUI automatically ...

    Your expectation is wrong. In ReactiveUI, the view is resolved based on the view model (as opposed to the view model being resolved by the view using a view model locator in Prism).

    Does ReactiveUI provide such service which set view model when view become activated?

    No. You should create the view model yourself and let ReactiveUI resolve the corresponding view. Check out the Routing section in the docs for an example.