Getting Unity to View Permissions in XAML

I start with MVVM and I begin to understand things. I am currently experimenting with a Cinch framework, although I'm not ready for this yet. I injected ViewModels into views using a link to the ViewModel in the view code, with a property having [Dependency] on it, and in setter it sets the DataContext to the correct view using Unity. I thought it was a trick.

I'm trying to get my application to work as a single window, with the entered views (as opposed to several windows and working with opening / closing) I changed my views from Windows to UserControls and added a to the main window. This worked, but the ViewModel was never entered, apparently because XAML does not use Container.Resolve to create the view, since when I created the view and added it manually in the code using Resolve, [Dependency] was created.

How to configure my window, so if I add a view via XAML or change the view as a result of the user interface, etc., it will receive it through Unity so that it can work with its magic?

+4
source share
2 answers

The way to solve your problem is to make your window also ViewModel, and ViewModels of UserControls provides properties in it. Then in your XAML for the window, you simply use the binding mechanism to bind the DataContexts UserControl to the corresponding properties of your main ViewModel. And since this main ViewModel is allowed from the Unity container, it will have all the other ViewModel-s that will be injected as needed.

+3
source

This problem is usually solved with the help of regions and RegionManager. In the main ViewModel window, a set of Regions is created and added to the RegionManager. ViewModels can then be enabled and added to the Region.Views collection.

In XAML, a region is typically entered using the ItemsSource property of the ItemsControl element associated with the region property of the main ViewModel.

So, on the main ViewModel screen, you will 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 will be normally enabled in your IModule.

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

And the XAML representation of your template, similar to

  <DataTemplate DataType="{x:Type TestModule:TestScreenViewModel}"> <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"> <StackPanel> <ItemsControl ItemsSource="{Binding Path=MainRegion.Views}" /> </StackPanel> </ScrollViewer> </DataTemplate> 
+4
source

All Articles