How do I load controls in different ContentControls of a Shell using CaliburnMicro

后端 未结 3 963
渐次进展
渐次进展 2020-12-18 00:47

By default when you use \"ActivateItem(new Control());\" your control is loaded into a ContentControl which with the name ActiveItem, fro example. . If I have multiple conte

3条回答
  •  旧巷少年郎
    2020-12-18 01:33

    This is an old question, but in case anyone is having the same issue, here is my solution:

    1. Your main window that contain both (or even more than two) of your User Controls must be inherited from Caliburn.Micro.Conductor.Collection.AllActive;
    2. Your User Controls must be inherited from Caliburn.Micro.Screen;
    3. You must also keep naming conventions in mind. If you use MenuUC as the name of a ContentControl in your View, also create a property named MenuUC in your ViewModel;
    4. Initialize your UserControl as I do in Constructor;
    5. Now you can use ActivateItem(MenuUC) and DeactivateItem(MenuUC) everywhere in your code. Caliburn.Micro automatically detects which one you want to work with.

    Example XAML View code:

    
    
        
            
                
                
            
            
                
                
            
    
            
            
    
            
            
                
            
        
    
    

    Example C# ViewModel code:

    class YourViewModel : Conductor.Collection.AllActive
    {
        // Menu Side Bar
        private MenuUCViewModel _menuUC;
        public MenuUCViewModel MenuUC
        {
            get { return _menuUC; }
            set { _menuUC = value; NotifyOfPropertyChange(() => MenuUC); }
        }
    
        // Panel
        private Screen _panelUC;
        public Screen PanelUC
        {
            get { return _panelUC; }
            set { _panelUC = value; NotifyOfPropertyChange(() => PanelUC); }
        }
    
        // Constructor
        public YourViewModel()
        {
            MenuUC = new MenuUCViewModel();
            ActivateItem(MenuUC);
    
            PanelUC = new FirstPanelUCViewModel();
            ActivateItem(PanelUC);
        }
    
        // Some method that changes PanelUC (previously FirstPanelUCViewModel) to SecondPanelUCViewModel
        public void ChangePanels()
        {
            DeactivateItem(PanelUC);
            PanelUC = new SecondPanelUCViewModel();
            ActivateItem(PanelUC);
        }
    }
    

    In the above example, ChangePanels() acts as a method to load new User Control into your ContentControl.

    Also read this question, it might be help you further.

提交回复
热议问题