How to use ReactiveUI inside a PRISM module

ぐ巨炮叔叔 提交于 2019-12-04 19:57:47

Prism and RxUI and different semantics on view binding.

Prism tries to resolve a VM type for a given view, whereas RxUI does it the other way around and resolves View for the current VM.

Your implementation is (almost) working:

this.RegionManager.RegisterViewWithRegion("RightRegion", typeof(HelloWorldView));

This is enough to display the view, and if you add AutoWireViewModel=True, Prism will create a VM instance and set it to your view DataContext.

Beware that this line:

this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);

Attempts to bind the ViewModel property to the same DataContext, and will happily do so with null, overriding the instance created/set by Prism (here's your issue I believe).

Simply removing this binding works, but overall it doesn't seem a nice way of mixing Prism and RxUI.

Maybe instead you could look into RxUI RoutedViewHost and ViewModelViewHost and bind one of these into the region instead. Then RxUI view resolving will kick in automatically, and refresh the inner views depending on the current VM set on the control. This is when you need to register your views, like you did:

Locator.CurrentMutable.Register(() => new HelloWorldView(), typeof(IViewFor<HelloWorldViewModel>));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!