c# - 将多个ViewModel用于RibbonWindow(带有或不带有Caliburn Micro)

标签 c# wpf mvvm ribbon caliburn.micro

我正在使用MVVM开发功能区WPF应用程序。功能区使用API​​ Fluent功能区控制套件; MVVM框架使用Caliburn Micro,DI使用Ninject。

我观察到的是,当应用程序增长时,用于 View MainView的MainViewModel也不断增长。第一个问题:是否有可能将MainViewModel分为多个VM?或者,多个VM控制包含丝带的同一MainView?

第二个问题是在这种情况下:

当用户按下功能区中的按钮时,功能区下方的区域显示不同的 View ,即“ActiveItem”更改为其他 View ,例如GlobalDataView或BreakdownDataView等。

    <Grid x:Name="SubView" Background="White">
        <ContentControl x:Name="ActiveItem" />
    </Grid>

假设现在已激活GlobalDataView(已分配到ContentControl中),则在 View 激活时将显示一个上下文功能区选项卡,并且如果问题1有很好的解决方案,则我们假定上下文功能区位于VM“ContextualRibbonGlobalDataViewModel”中。另外,GlobalDataView本身也有自己的VM,称为“GlobalDataViewModel”。我的问题是:是否可以合并ContextualRibbonGlobalDataViewModel和GlobalDataViewModel?由于它们都包含非常特定于GlobalDataView的逻辑

如果Q1不可能,是否有办法让GlobalDataViewModel控制MainView中的上下文功能区?

我当前的方法是MainViewModel(MainView)+ GlobalDataViewModel(GlobalDataView),这样MainViewModel包含用于GlobalData View 的上下文功能区的所有命令(CM中的操作)。 MainViewModel必须使用中介程序(CM中的EventAggregator)与GlobalDataViewModel进行对话。

谢谢。

最佳答案

在我的应用程序中,我有一个“ShellView”主模型,其中包含多个子容器。因此,这是完全可能的,特别是如果子项目在逻辑上相关并且与其他部分分开。
学习如何做有点麻烦,但是归结为:

    [ImportingConstructor]
    public ShellViewModel(IWindowManager windowManager, IEventAggregator events)
    {
        this.windowManager = windowManager;
        this.logger = NLog.LogManager.GetCurrentClassLogger();
        this.events = events;
        events.Subscribe(this);
    }

加载另一个 View :
  public async void loadview()
  {
    this.ActivateItem(new BlaViewModel(this.events, this.windowManager));
    //displaying it in a tabcontrol, which is defined in XAML. It would probably work with something else too
    var thisView = (ShellView)this.GetView();
    thisView.BlaViewM.SelectedItem
}

在新 View 中:
public PowerPointViewModel(, IEventAggregator events, IWindowManager windowManager)
{
    this.logger = NLog.LogManager.GetCurrentClassLogger();

    events.Subscribe(this);
    this.events = events;
    this.DisplayName = "BLA";

    this.windowManager = windowManager;
}

我使用事件来传递消息,特别是如果需要在单独的 View 模型之间进行交互时。
从作者那里可以找到一个不错的教程:
http://devlicio.us/blogs/rob_eisenberg/archive/2010/10/08/caliburn-micro-soup-to-nuts-part-6a-screens-conductors-and-composition.aspx

您能否详细说明第二个问题?我不确定我是否正确理解您。
但是我认为您的功能区中的逻辑太笨拙了。我的功能区只是启动事件,这些事件被发送到特定的 View 模型,然后可以与数据访问层进行通信。因此,我的功能区也特定于某个Viewmodel。但是,他们没有必要做很多事情。
Som认为像下拉列表一样依靠Viewmodel方法来获取要显示的值,但它们无能为力。

关于c# - 将多个ViewModel用于RibbonWindow(带有或不带有Caliburn Micro),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139555/

相关文章:

c# - 更好的 Linq 查询,用于过滤没有子项的父列表

c# - 在绘图上使用graphics.ScaleTransform

c# - 通过 linq2sql 选择 n 随机行

WPF如何将混合交互触发器添加到样式资源

c# - 如何禁用通过 COM 公开的 WPF HwndSources 的 DPI 缩放?

c# - 在 WPF 项目中,如何初始化窗口和控件,然后跳转到主循环?

WPF 在 MVVM 中使用 SaveFileDialog

c# - ulong 和 long 之间不可能的比较突然成为可能

.net - 推荐的 WPF 项目结构?

android - 我可以获取 ViewModel 的 init block 内的上下文吗?