Getting Unity to Resolve views in XAML

前端 未结 2 1925
故里飘歌
故里飘歌 2021-01-06 09:13

I\'m starting out with MVVM, and I\'m starting to understand things. I\'m currently experimenting with the Cinch framework, though I\'m not committed to it as of yet.
I

2条回答
  •  感动是毒
    2021-01-06 09:57

    This problem is normally solved using Regions and the RegionManager. In the main window ViewModel, a set of Regions is created and added to the RegionManager. Then ViewModels can be Resolved and added to the Region.Views collection.

    In XAML, the Region is normally injected by having the ItemsSource property of an ItemsControl bound to the region property of the main ViewModel.

    So, in the main screen ViewModel you would have something like this:

        public class TestScreenViewModel
    {
        public const string MainRegionKey = "TestScreenViewModel.MainRegion";
    
        public TestScreenViewModel(IUnityContainer container, IRegionManager regionManager)
        {
            this.MainRegion = new Region();
            regionManager.Regions.Add(MainRegionKey, this.MainRegion);
        }
    
        public Region MainRegion { get; set; }
    }
    

    This would be Resolved normally in your IModule

            #region IModule Members
    
        public void Initialize()
        {
            RegisterViewsAndServices();
    
            var vm = Container.Resolve();
            var mainScreen = Container.Resolve();
            mainScreen.MainRegion.Add(vm);
    
            var mainView = ContentManager.AddContentView("Test harness", mainScreen);
        }
    
        #endregion
    

    And the XAML representation of your template looking something like

        
        
            
                
            
        
    
    

提交回复
热议问题