开发者

Is it right to call Navigate to other page from ViewModel?

I know ther开发者_运维百科e is many ways to Navigate to other page from ViewModel.

I'm not sure is this a good idea or not, since MVVM's purpose is to separate View from ViewModel, but Navigate to other page within ViewModel mean there's a couple between View and ViewModel, that being said, if I change View name, I need to change URI (string) in ViewModel too.

I want to know, is it right to call Navigate to other page from ViewModel? If it's wrong, what's the correct (or better) way to navigate to other page?


It's both right and natural.

When you make a navigation request, the "name" you provide as part of the Uri is the name with which your View class has been registered in the container. Since registering classes with the container is done by the module itself and not the View or the ViewModel, you are not introducing coupling between the View and ViewModel.

That said, if you want to customize the way that Prism interprets your navigation requests, you can always write a class implementing IRegionNavigationContentLoader. If you register this into the container, Prism will resolve your own class instead of the built-in RegionNavigationContentLoader. But it's not the simplest thing in the world to do.


Why don't you create a separate class that will keep the logic for navigation? In that case you'll only need to send a message from ViewModel that program should change the View and details would be decided arbitrally. With that approach you still have no direct connection between View and VM

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜