简体   繁体   English

包含两个Caliburn.Micro视图的对话视图?

[英]Dialog view containing two Caliburn.Micro views?

I have a Windows WPF app in which I'm using Caliburn.Micro. 我有一个Windows WPF应用程序,我正在使用Caliburn.Micro。 The main window view/viewmodel is handled by Caliburn.Micro. 主窗口视图/ viewmodel由Caliburn.Micro处理。 One of it's buttons pops up a new dialog window which uses a different view-viewmodel. 其中一个按钮弹出一个新的对话窗口,该窗口使用不同的视图模型。

In this dialog I have a container (list box and some filter controls) that I want to make into a reusable control so that I can include it in other dialogs. 在这个对话框中,我有一个容器(列表框和一些过滤器控件),我想将其作为一个可重用的控件,以便我可以将它包含在其他对话框中。 To do this I extracted from the dialog's view and viewmodel the relevant code and created a new view and viewmodel. 为此,我从对话框的视图中提取并viewmodel相关代码并创建了一个新视图和viewmodel。 This all looks good. 这一切看起来都不错。

The problem is that now back in the dialog I have a dockpanel with a big empty space where I need the reusable control to go. 问题是,现在回到对话框中我有一个带有大空空间的dockpanel,我需要可重用的控件。

From the dialog viewmodel OnInitalize() I could create the reusable control viewmodel, but I don't know how to get it's view into the dialog view's dockpanel. 从对话框viewmodel OnInitalize()我可以创建可重用的控件视图模型,但我不知道如何将它的视图放入对话框视图的dockpanel。

To create the dialog from the main window viewmodel I use WindowManager().ShowDialog() to display the viewmodel for the dialog and Caliburn.Micro takes care of setting up the view. 要从主窗口viewmodel创建对话框,我使用WindowManager()。ShowDialog()显示对话框的viewmodel,Caliburn.Micro负责设置视图。 Is there a way I can specify in the dialog's XAML that I want to embed the view for the reusable control and have Caliburn create the appropriate view/viewmodel? 有没有一种方法可以在对话框的XAML中指定我想为可重用控件嵌入视图并让Caliburn创建适当的视图/视图模型?

Or am I going about it the wrong way? 或者我是以错误的方式去做的?

If you have a property on your dialog view model which is another view model type, and you add a ContentControl to your dialog view which is named the same as this property, then Caliburn.Micro will automatically inject the view which corresponds to your property view model type into the ContentControl placeholder, and bind that view model type to the view automatically too. 如果对话框视图模型上有一个属性是另一个视图模型类型,并且您将ContentControl添加到对话框视图中,该视图的名称与此属性相同,则Caliburn.Micro将自动注入与您的属性视图对应的视图将模型类型放入ContentControl占位符,并自动将该视图模型类型绑定到视图中。 Is this what you mean? 你是这个意思吗? Something like: 就像是:

// Dialog View Model
private MyReusableControlViewModel myReuseableControl;
public MyReusableControlViewModel MyReuseableControl
{ 
   get { return this.myReuseableControl; }
   set { this.myReuseableControl = value;  NotifyOfPropertyChanged(...); }
}

// Dialog View Model Constructor
public DialogViewModel()
{
  this.MyReuseableControl = new MyReusableControlViewModel();
}

// Dialog View
<DockPanel>
  ...
  <ContentControl x:Name="MyReusableControl" />
</DockPanel>

Of course, ideally you would want to inject any dependencies of the dialog view model (in this case MyReusableControlViewModel) and work against abstractions inside the dialog view model, rather than concrete types. 当然,理想情况下,您希望注入对话框视图模型的任何依赖项(在本例中为MyReusableControlViewModel),并在对话框视图模型中处理抽象,而不是具体类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM