这主要取决于您的应用程序的外观(即同时打开多少个窗口,是否有模态窗口......等等)。
我给出的一般建议是不要尝试做“pure”MVVM;我经常读到诸如“应该有零代码隐藏”之类的东西,我不同意。
我目前正在将我的项目分离为模型程序集/项目,
ViewModel 程序集/项目和视图程序集/项目。这应该去
进入不同的“核心”程序集?
将视图和 ViewModel 分离到不同的程序集中是您可以做的最好的事情,以确保您永远不会引用与 viewModel 中的视图相关的内容。这种强烈的分离会很好。
使用两个不同的程序集将模型与 ViewModel 分离也是一个好主意,但这取决于您的模型的外观。我个人喜欢 3 层架构,所以我的模型通常是 WCF 客户端代理,并且确实存储在它们自己的程序集中。
无论如何,“核心”程序集总是一个好主意(恕我直言),但只是为了公开可以在应用程序的所有层中使用的基本实用程序方法(例如基本扩展方法......等)。
现在对于您关于视图的问题(如何显示它们......等等),我会说做简单的。就我个人而言,我喜欢在我的视图的代码隐藏中实例化我的视图模型。我也经常在我的 ViewModel 中使用 events,以便通知关联的视图它应该打开另一个视图。
例如,当用户单击按钮时,您的 MainWindow 应该显示一个子窗口:
// Main viewModel
public MainViewModel : ViewModelBase
{
...
// EventArgs<T> inherits from EventArgs and contains a EventArgsData property containing the T instance
public event EventHandler<EventArgs<MyPopupViewModel>> ConfirmationRequested;
...
// Called when ICommand is executed thanks to RelayCommands
public void DoSomething()
{
if (this.ConfirmationRequested != null)
{
var vm = new MyPopupViewModel
{
// Initializes property of "child" viewmodel depending
// on the current viewModel state
};
this.ConfirmationRequested(this, new EventArgs<MyPopupViewModel>(vm));
}
}
}
...
// Main View
public partial class MainWindow : Window
{
public public MainWindow()
{
this.InitializeComponent();
// Instantiates the viewModel here
this.ViewModel = new MainViewModel();
// Attaches event handlers
this.ViewModel.ConfirmationRequested += (sender, e) =>
{
// Shows the child Window here
// Pass the viewModel in the constructor of the Window
var myPopup = new PopupWindow(e.EventArgsData);
myPopup.Show();
};
}
public MainViewModel ViewModel { get; private set; }
}
// App.xaml, starts MainWindow by setting the StartupUri
<Application x:Class="XXX.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
StartupUri="Views/MainWindow.xaml">