请用C#给出在运用MVVM模式设计WPF时,如何实现指定初始窗口或页面,给出具体完整的实例代码
时间: 2024-05-06 14:17:17 浏览: 148
在MVVM模式下,我们可以使用ViewModel First的方式来指定初始窗口或页面。具体实现步骤如下:
1. 在App.xaml中指定默认的StartupUri为一个空白窗口,如下所示:
```xml
<Application x:Class="MVVMStartupUri.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMStartupUri"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
```
2. 在MainWindow.xaml中定义一个ContentControl,并将其Content属性绑定到一个ViewModel的实例,如下所示:
```xml
<Window x:Class="MVVMStartupUri.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMStartupUri"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ContentControl Content="{Binding CurrentViewModel}" />
</Grid>
</Window>
```
3. 在MainWindow.xaml.cs中定义一个MainViewModel的实例,并将其赋值给MainWindow的DataContext属性,如下所示:
```csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Create and set the DataContext
DataContext = new MainViewModel();
}
}
```
4. 在MainViewModel中定义一个CurrentViewModel属性,用于表示初始窗口或页面对应的ViewModel,如下所示:
```csharp
public class MainViewModel : INotifyPropertyChanged
{
private ViewModelBase _currentViewModel;
public MainViewModel()
{
// Set the initial view model
CurrentViewModel = new InitialViewModel();
}
public ViewModelBase CurrentViewModel
{
get { return _currentViewModel; }
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
5. 在InitialViewModel中定义初始窗口或页面的逻辑,如下所示:
```csharp
public class InitialViewModel : ViewModelBase
{
// Define the initial window or page
}
```
6. 在ViewModelBase中实现INotifyPropertyChanged接口,如下所示:
```csharp
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
完整的实例代码如下:
App.xaml
```xml
<Application x:Class="MVVMStartupUri.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMStartupUri"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
```
MainWindow.xaml
```xml
<Window x:Class="MVVMStartupUri.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMStartupUri"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ContentControl Content="{Binding CurrentViewModel}" />
</Grid>
</Window>
```
MainWindow.xaml.cs
```csharp
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Create and set the DataContext
DataContext = new MainViewModel();
}
}
```
MainViewModel.cs
```csharp
public class MainViewModel : INotifyPropertyChanged
{
private ViewModelBase _currentViewModel;
public MainViewModel()
{
// Set the initial view model
CurrentViewModel = new InitialViewModel();
}
public ViewModelBase CurrentViewModel
{
get { return _currentViewModel; }
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
InitialViewModel.cs
```csharp
public class InitialViewModel : ViewModelBase
{
// Define the initial window or page
}
```
ViewModelBase.cs
```csharp
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
阅读全文