here is a small example of how I use Unity and Template 10.
1. Create a ViewModel
I also created a DataService class to create a list of people. Take a look at the [Dependency] annotation.
public class UnityViewModel : ViewModelBase { public string HelloMessage { get; } [Dependency] public IDataService DataService { get; set; } private IEnumerable<Person> people; public IEnumerable<Person> People { get { return people; } set { this.Set(ref people, value); } } public UnityViewModel() { HelloMessage = "Hello !"; } public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState) { await Task.CompletedTask; People = DataService.GetPeople(); } }
2. Create a class to create and populate your UnityContainer
Add UnityViewModel and DataService to unitContainer. Create a property to enable UnityViewModel.
public class UnitiyLocator { private static readonly UnityContainer unityContainer; static UnitiyLocator() { unityContainer = new UnityContainer(); unityContainer.RegisterType<UnityViewModel>(); unityContainer.RegisterType<IDataService, RuntimeDataService>(); } public UnityViewModel UnityViewModel => unityContainer.Resolve<UnityViewModel>(); }
3. Add UnityLocator to your app.xaml
<common:BootStrapper x:Class="Template10UWP.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:common="using:Template10.Common" xmlns:mvvmLightIoc="using:Template10UWP.Examples.MvvmLightIoc" xmlns:unity="using:Template10UWP.Examples.Unity"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles\Custom.xaml" /> <ResourceDictionary> <unity:UnitiyLocator x:Key="UnityLocator" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
4. Create page
Use UnityLocator to set UnityViewModel as DataContext and bind properties to controls
<Page x:Class="Template10UWP.Examples.Unity.UnityMainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Template10UWP.Examples.Unity" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" DataContext="{Binding Source={StaticResource UnityLocator}, Path=UnityViewModel}" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Text="{Binding HelloMessage}" HorizontalAlignment="Center" VerticalAlignment="Center" /> <ListBox Grid.Row="1" ItemsSource="{Binding People}" DisplayMemberPath="FullName"> </ListBox> </Grid>
The DataService will be automatically entered when the page allows the UnityViewModel.
Now for your questions
It depends on how the projects depend on each other. I'm not sure if this is the best solution, but I think I will try to use one UnityContainer and put it in the main library.
I hope my examples answered this question
I hope my examples answered this question
AK
source share