【问题标题】:Caliburn Micro - Show Screens simultaniouslyCaliburn Micro - 同时显示屏幕
【发布时间】:2015-09-11 09:05:39
【问题描述】:

我知道如果我执行以下操作,我可以在屏幕之间切换:

ShellViewModel.cs

public class ShellViewModel : Conductor<object>.Collection.OneActive
{
  public void ShowFirstScreen()
  {
    ActivateItem(new FirstViewModel());
  }

  public void ShowSecondScreen()
  {
    ActivateItem(new SecondViewModel());
  }

}

用于 OneActive 的 ShellView.xaml

<ContentControl x:Name="ActiveItem" />

但我想同时显示这些屏幕。因此,当从 OneActive 更改为 AllActive 时,屏幕不再可见。

我知道如果我使用 ItemsControl 可以显示这些屏幕

AllActive 的 ShellView.xaml

<ItemsControl ItemsSource="{Binding Path=Items}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ContentControl cal:View.Model="{Binding}"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

但我想将它们随机放置在我的 ShellView.xaml 中。

怎么做?

如何访问它们?我想将它们像 UserControls 一样放在我的 ShellView.xaml 中。

<Grid>
<ContenControl x:Name="Item1 or even the ViewModel name"/>
<ContenControl x:Name="Item2 or even the ViewModel name"/>
...
</Grid>

【问题讨论】:

    标签: c# wpf xaml caliburn.micro


    【解决方案1】:

    在你的 shellview.xaml 中

    <ContentControl x:Name="NameOfViewModel" />
    

    在您的 ShellViewModel.cs 中

     public NameOfViewModel NameOfViewModel {
       get;set;  // auto for brevity 
     }
    

    在您选择的 OnInitialized()、OnActivated() 或 Ctor 中实例化视图模型...基本上,如果 ContentControl 名称与某个属性匹配,则在实例化视图模型后,您将获得一个屏幕。没有匹配没有屏幕。

    【讨论】:

    • 谢谢。如果我将 ViewModel 的名称从 NameOfViewModel 更改为 NameOfViewModel,为什么这不起作用? x:Name 属性中不允许使用“_”吗?
    • msdn.microsoft.com/en-us/library/vstudio/… 必须符合 XamlGrammar,我不相信以 _ 为前缀会起作用。
    • 尝试了和上面一样的方法,它初始化了 Viewmodel 构造函数但没有显示 View /initialize View 构造函数。
    猜你喜欢
    • 2018-03-15
    • 2017-06-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2013-05-01
    • 2012-01-29
    • 2012-10-24
    相关资源
    最近更新 更多