here is a small example 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 fill your UnityContainer
Add the UnityViewModel and the DataService to the unityContainer.
Create a property to resolve the 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 the 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>
<!-- custom resources go here -->
</ResourceDictionary>
</Application.Resources>
4. Create the page
Use the UnityLocator to set the UnityViewModel as DataContext and bind the properties to the 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 injected automatically when the page resolves the UnityViewModel.
Now to your questions
This depends on how the projects depend on each other. I am not sure what´s the best solution, but i think i would try to use one UnityContainer and place it in the core library.
I hope my examples answered this question
I hope my examples answered this question