I am using Prism. I have a view(DetailMainUC.xaml) which hold many other views in the following way.
<UserControl.Resources>
<DataTemplate x:Key="View1Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:HomeUC />
</DataTemplate>
<DataTemplate x:Key="View2Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:WalkAwayBehaviorUC />
</DataTemplate>
<DataTemplate x:Key="View3Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:WakeUpOnApproachUC />
</DataTemplate>
<DataTemplate x:Key="View4Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:NoLockOnPresenceUC />
</DataTemplate>
<DataTemplate x:Key="View5Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:PeekDimmingUC />
</DataTemplate>
<DataTemplate x:Key="View6Template" DataType="{x:Type vm:DetailMainUCViewModel}">
<local:SettingsUC />
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl Grid.Column="1" Grid.Row="1" Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SwitchView}" Value="Home">
<Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="Walk Away Behaviour">
<Setter Property="ContentTemplate" Value="{StaticResource View2Template}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="Wake up on approach">
<Setter Property="ContentTemplate" Value="{StaticResource View3Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="No lock on presence">
<Setter Property="ContentTemplate" Value="{StaticResource View4Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="Peeking and Dimming">
<Setter Property="ContentTemplate" Value="{StaticResource View5Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="Settings">
<Setter Property="ContentTemplate" Value="{StaticResource View6Template}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
Now I am using event aggregator to communicate between different view models in other regions. My view model for the above usercontrol is as follows:
public DetailMainUCViewModel(IEventAggregator eventAggregator, HomeViewModel homeViewModel)
{
this.HomeVM = homeViewModel;
this._eventAggregator = eventAggregator;
HomeVM.Initialization(this._eventAggregator);
this._eventAggregator.GetEvent<MenuClickEvent>().Subscribe(GetMenuName);
SwitchView = "Home";
}
Now the property HomeVM is of type HomeViewModel which is view model of child DetailMainUCViewModel. Like this there are many other child view models are there. Now my problem is I am seeing constructor of HomeViewModel is getting called twice and same is happening for all other child view models. the main problem I am facing is while child view models are getting called second times , the eventAggregator is becoming null.
DetailsMainUCViewModel is parent viewmodel and HomeViewModel is child viewmodel. DetailMainUC.xaml holds the HomeUC.xaml in the way as mentioned in code section. I have written the below mentioned code too
<UserControl.DataContext>
<vm:HomeViewModel></vm:HomeViewModel>
</UserControl.DataContext>
I suspect as I am using two places to attach datacontext so my viewmodel is getting called two times. in 1.HomeUC.xaml
2.Parent usercontrol DetailMainUC.xaml
But I am unable to remove them as it is needed. My class design is DetailMainUCViewModel is parent viewmodel and HomeViewModel is child viewmodel. DetailMainUC.xaml is parent view and HomeUC.xaml is child view. at the beginning the code snippet which I have given is from DetailMainUC.xaml (which holds all child usercontrols)
apart from this I am initializing my DetailMainUC.xaml in module where I have implemented IModule interface.
public class STDetailOptionsModule : IModule
{
private readonly IRegionManager _regionManager;
public STDetailOptionsModule(IRegionManager regionManager)
{
this._regionManager = regionManager;
}
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion(RegionNames.DetailOptionsRegion, typeof(DetailMainUC));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
regards