i using prism with c# an there i have a problem with the load of two modules. In module A is a TabControl with a ItemTemplate and ContentTemplate. Module B is a Subview of module A and is in the ContentTemplate of Module A.
Xaml of Module A
<Grid>
<TabControl x:Name="tabControl" ItemsSource="{Binding Folders}" SelectedItem="{Binding SelectedItem}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl prism:RegionManager.RegionName="ListModulRegion" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
ViewModel of Module A
In the ViewModel of Module A is a eventAggregator. Which publish a event when a other item in the TabControll is selected.
#region Private Fields **************************************************************************************************
private readonly IDeviceService _deviceService;
private readonly IEventAggregator _eventAggregator;
private ModuleFolder _selectedItem;
#endregion
#region Public Properties ***********************************************************************************************
public IEnumerable<ModuleFolder> Folders
{
get
{
return _deviceService.ModuleFolders;
}
}
public ModuleFolder SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged(() => SelectedItem);
ModuleFolderChangeEvent evt = _eventAggregator.GetEvent<ModuleFolderChangeEvent>();
evt.Publish(SelectedItem);
}
}
#endregion
#region Constructor(s) **************************************************************************************************
public FolderSelectionViewModel(IEventAggregator eventAggregator, IDeviceService deviceService)
{
_eventAggregator = eventAggregator;
_deviceService = deviceService;
if(_deviceService.ModuleFolders.Count>0)
{
SelectedItem = _deviceService.ModuleFolders[0];
}
}
ViewModel of Module B
In the ViewModel of Module B the eventAggregator is subscribte to this event.
#region Private Fields **************************************************************************************************
private ModuleFolder _currentFolder;
private readonly IEventAggregator _eventAggregator;
private readonly IDeviceService _deviceService;
private readonly IUiDispatcher _uiDispatcher;
private ObservableCollection<ModuleViewModel> _observableModules;
private ModuleViewModel _selectedListItem;
#endregion
#region Public Properties ***********************************************************************************************
public ObservableCollection<ModuleViewModel> ObservableModules
{
get
{
return (_observableModules);
}
set
{
_observableModules = value;
OnPropertyChanged(() => ObservableModules);
}
}
public ModuleViewModel SelectedListItem
{
get { return _selectedListItem; }
set
{
_selectedListItem = value;
OnPropertyChanged(() => SelectedListItem);
}
}
#endregion
#region Constructor(s) **************************************************************************************************
public ModuleListViewModel(IEventAggregator eventAggregator, IUiDispatcher uiDispatcher, IDeviceService deviceService)
{
ModuleFolderChangeEvent evt = eventAggregator.GetEvent<ModuleFolderChangeEvent>();
evt.Subscribe(OnMailFolderChanged);
_eventAggregator = eventAggregator;
_uiDispatcher = uiDispatcher;
_deviceService = deviceService;
PropertyChanged += ModuleListSelectionViewModel_PropertyChanged;
}
#endregion
Bootstrapper
In the Bootsrapper module A depends on modul B.
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog) ModuleCatalog;
//Communication
Type serviceModule = typeof(CommunicationModule);
moduleCatalog.AddModule(serviceModule);
//Device List
Type deviceListUiModule = typeof(DeviceListModule);
System.Collections.ObjectModel.Collection<string> dependsDeviceListUi = new System.Collections.ObjectModel.Collection<string>
{
serviceModule.Name
};
moduleCatalog.AddModule(new ModuleInfo
{
ModuleName = deviceListUiModule.Name,
ModuleType = deviceListUiModule.AssemblyQualifiedName,
DependsOn = dependsDeviceListUi,
});
//Tap Region
Type selectionUiModule = typeof (SelectionModule);
System.Collections.ObjectModel.Collection<string> dependsSelectionUi = new System.Collections.ObjectModel.Collection<string>
{
serviceModule.Name,
deviceListUiModule.Name
};
moduleCatalog.AddModule(new ModuleInfo
{
ModuleName = selectionUiModule.Name,
ModuleType = selectionUiModule.AssemblyQualifiedName,
DependsOn = dependsSelectionUi
});
}
The Problem
When module A is loaded. The first item is selected and publish an event. But module B is not loaded an subscribe to late to the event an miss the first publish. At the second time when module B is loaded every thing works fine.
I have read about How to control the order of module initialization in Prism. But maybe there is another way or better way of doing that. Because i'm new in prism and i dont now the best way to doing it.
Sorry for my poor english. :-(