您可以创建一个单独的服务来将视图作为对话框启动,以便可以在整个应用程序中以通用方式使用它。并将通过想要启动任何对话框的构造函数将此服务注入到 ViewModel 中。
public interface IDialogWindowService<T>
{
void Show();
void ShowDialog();
}
public class DialogWindowService<T> : IDialogWindowService<T> where T : Window
{
public void Show()
{
container.Resolve<T>().Show();
}
public void ShowDialog()
{
container.Resolve<T>().ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
现在只需将此服务注入到相应的 ViewModel 中即可。
public class YourViewModel
{
//commands
public ICommand someCommand { get; set; }
private IDialogWindowService<BookingView> _dialogService;
public YourViewModel(IDialogWindowService<YourView > dialogService)
{
_dialogService = dialogService
someCommand = new RelayCommand(someCommandDoJob, () => true);
}
public void someCommandDoJob(object obj)
{
//Since you want to launch this view as dialog you can set its datacontext in its own constructor.
_dialogService.ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
或者
您可以使用DataTemplates来更改视图。它允许Views根据以下情况动态切换ViewModel:
<Window>
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModelA}">
<localControls:ViewAUserControl/>
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModelB}">
<localControls:ViewBUserControl/>
</DataTemplate>
<Window.Resources>
<ContentPresenter Content="{Binding CurrentView}"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
如果 Window.DataContext 是 ViewModelA 的实例,则将显示 ViewA 并且
Window.DataContext是ViewModelB的实例,那么ViewB就会被显示。
我见过和读过的最好的例子是 Rachel Lim 制作的。请参阅示例。
根据您的使用情况,从视图的代码隐藏中打开视图没有任何问题。毕竟还是查看代码。
MyView view = new MyView();
view.Show();
Run Code Online (Sandbox Code Playgroud)
ViewModel否则,如果您需要从或 使用 a打开窗口ICommand,那么您可以查看我在GitHub上编写的“在 MVVM 中打开窗口和对话框”库。这将演示如何使用 MVVM 设计模式通过单击按钮来打开。Window