简体   繁体   中英

Switching between ViewModels in Caliburn Micro

I am trying to make a WPF application using Caliburn Micro. I have a ShellView with a ContentControl in it on the full page. I am already showing on startup an UserControl in the ShellView s ContentControl(Basically it is a LogIn Page). After login, I want to close the current ViewModel and show another one in the ShellView s ContentControl. How can I do this?

You need to begin by inheriting your ShellViewModel from Conductor class and other ViewModels (Login and SecondViewModel) from Screen. You can read more on Screen and Conductors . For example,

 public class ShellViewModel:Conductor<Screen> 

 public class UserControl1ViewModel: Screen 

 public class UserControl2ViewModel: Screen

The ShellViewModel would be conducting between different Screens and has inherited from Caliburn.Micro's Conductor class. When you show a screen, the conductor makes sure it is properly activated. If you are transitioning away from a screen, it makes sure it gets deactivated.

The second change you need to make is in the Context Control in ShellView by binding it to Conductor's Active Item.

<ContentControl x:Name="ActiveItem"/>

Finally, you could make use of Conductor's ActivateItem method to switch between screens.

 ActivateItem(new UserControl2ViewModel());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM