在不创建过于复杂的答案的情况下,也不包括对您来说显然是新的概念,例如DelegateCommand (s) 或WindowManager (s),这是一个全屏应用程序的简单示例,显示了许多不同的“子窗口”(不是Windows 本身,而是 UserControls)
主窗口:
<Window x:Class="FullScreenAppSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowState="Maximized"
WindowStyle="None">
</Window>
代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ShowLogin()
{
var loginview = new LoginView();
this.Content = loginview;
}
private void ShowMenu()
{
var menu = new MenuView();
this.Content = menu;
}
}
登录视图:
<UserControl x:Class="FullScreenAppSample.Login.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- your login screen UI here -->
</UserControl>
菜单视图:
<UserControl x:Class="FullScreenAppSample.Menu.MenuView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- your Menu UI here -->
</UserControl>
这就是所谓的“视图优先”方法,其中视图通过负责实例化其他视图并使它们可见来指示应用程序的“流程”。
话虽如此,我会花一点时间来解决你的断言
每个人的处理方式都不一样
是的。创建 WPF 应用程序的主流方法是一种称为MVVM的方法,它被设想为Martin Fowler 的 Presentation Model的 WPF 特定版本。然而,有许多解释和许多不同版本的 MVVM,以及许多MVVM 框架,例如MVVM Light、Caliburn.Micro和 Microsoft 的Prism(以及许多其他)。
这些框架中的每一个都提供了基本工具(基类、帮助类、服务、抽象、事件聚合器等等)来简化大规模、复杂 WPF 应用程序的开发。
底线:没有确定的“正确方法”来处理 WPF 中的 View 和 ViewModel 实例化/管理之类的事情,这取决于您选择的 MVVM 框架和项目的具体情况,例如对可测试性的需求。
我个人从几个不同的框架中提取了各种部分和组件(以及概念),并组成了我自己的 ViewModel-first MVVM 方法。我建议您花一些时间来分析您的项目需求并考虑是否使用其中任何一个。