C#prism中模块依赖性和eventAggregator对模块加载的问题

时间:2015-09-30 11:55:39

标签: c# wpf module prism eventaggregator

我使用pr#和c#,我有两个模块的负载问题。在模块A中是带有ItemTemplate和ContentTemplate的TabControl。模块B是模块A的子视图,位于模块A的ContentTemplate中。

模块A的Xaml

<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>

模块A的ViewModel

在模块A的ViewModel中是一个eventAggregator。当选择TabControll中的其他项目时发布事件。

    #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];
        }
    }

模块B的ViewModel

在模块B的ViewModel中,eventAggregator订阅此事件。

    #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

引导程序

在Bootsrapper模块中,A取决于模块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
        });
    }

问题

加载模块A时。选择第一个项目并发布事件。但是模块B没有加载订阅,因为错过了第一次发布的事件。第二次加载模块B时,每件事情都可以正常工作。

我读过How to control the order of module initialization in Prism。但也许还有另一种方法或更好的方法。因为我是棱镜新手,我现在不是最好的方法。

抱歉我的英语不好。 : - (

1 个答案:

答案 0 :(得分:0)

在bootstrap过程中,确保在“ModuleA”之前加载并初始化“ModuleB”。