如何动态创建功能区选项卡?

Lar*_*i13 6 c# ribbon prism mef mvvm

我想使用PrismV4,MEF,Ribbon开始开发新的应用程序.但现在,我有一个问题.如何动态创建功能区的选项卡?应用程序中的每个模块都可以在功能区中创建自己 每个标签可能有很多组.

怎么做到呢?我需要在哪里放置每个组的定义(使用什么控件(按钮,文本框,组合框等)和命令绑定以及如何?

我是否需要在Module中的某处编写XAML,或者所有这些都可以通过代码完成?最后一个问题,如何通知Ribbon(在Shell中)将这些选项卡添加到功能区?我应该使用EventAggregator从Module与Shell进行通信吗?要么?

Dan*_*rod 3

对于非上下文选项卡,解决此问题我最喜欢的方法是动态加载组件(例如通过反射),其中包含绑定到命令和 VM(或控制器)的 XAML,其中 VM(或控制器)包含命令实现并执行该命令绑定。

对于上下文选项卡,我最喜欢的方法是包含模型到 ViewModel 映射的字典,然后按名称激活/停用使用上述方法加载的上下文选项卡(并将正确的数据上下文传递给它们 - 视图模型)。

伪代码应该或多或少像这样(取决于您的框架实现的内容以及您必须自己实现的内容):

// Deserialize UI controllers from configuration files
// Each controller should act as view-model for its UI elements

// Register controllers with UI Manager
foreach controller in config.UiControllers uiManager.AddController(controller);


void UiManager.AddController(UiController controller)
{
    // Load controller's tool windows
    foreach toolWindow in contoller.toolWindows
    {
         toolWindow.LoadResources();
         toolWindow.DataContext = controller;
         mainWindow.AddToolWindow(toolWindow, contoller.PreferedUiRegion);
    }

    // Load controller's toolbars
    foreach toolBar in controller.ToolBars
    {
         toolBar.DataContext = controller;
         mainWindow.AddToolBar(toolBar);
    }

    // Load controller's contextual toolbar groups
    foreach group in controller.ContextualToolBarGroups
    {
         group.DataContext = controller;
         mainWindow.AddContextualToolBarGroupr(group);
    }

    // Load view models for specific model types
    foreach item in controller.ViewModelsDictionary
    {
         this.RegisterViewModelType(item.ModelType, item.ViewModelType, item.ViewType);
    }
}


void UiManager.OnItemSelected(Object selectedItem)
{
    var viewModelType = GetViewModelTypeFromDictionary(selectedItem);
    var viewType = GetViewTypeFromDictionary(selectedItem) ?? typeof(ContentPresentor);

    var viewModel = Reflect.CreateObject(viewModelType);
    var view = Reflection.CreateObject(viewType);

    viewModel.Model = selectItem;
    view.DataContext = viewModel;

    // Enable activation of contextual tab group on activate of view (if user clicks on it)
    view.OnActivatedCommandParameter = viewModel.ContextualTabGroupName;

    // This command should ask mainWindow to find contextual tab group, by name, and activate it
    view.OnActivatedCommand = ModelActivatedCommand;

    mainWindow.DocumentArea.Content = view;
}
Run Code Online (Sandbox Code Playgroud)