用WPF写一段多视图切换管理的代码
时间: 2024-05-10 08:20:03 浏览: 14
以下是一个简单的多视图切换管理的示例代码:
MainWindow.xaml:
```xaml
<Window x:Class="MultiViewSwitching.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiViewSwitching"
Title="Multi-View Switching" Height="300" Width="400">
<Window.Resources>
<DataTemplate x:Key="View1Template">
<local:View1/>
</DataTemplate>
<DataTemplate x:Key="View2Template">
<local:View2/>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl x:Name="ViewContainer" Content="{Binding CurrentView}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,20,0,0">
<Button Content="View 1" Command="{Binding SwitchToView1Command}"/>
<Button Content="View 2" Command="{Binding SwitchToView2Command}"/>
</StackPanel>
</Grid>
</Window>
```
MainWindow.xaml.cs:
```csharp
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace MultiViewSwitching
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
public class MainViewModel : INotifyPropertyChanged
{
private object currentView;
public object CurrentView
{
get { return currentView; }
set
{
currentView = value;
OnPropertyChanged(nameof(CurrentView));
}
}
public ICommand SwitchToView1Command { get; }
public ICommand SwitchToView2Command { get; }
public MainViewModel()
{
SwitchToView1Command = new RelayCommand(o => CurrentView = new View1());
SwitchToView2Command = new RelayCommand(o => CurrentView = new View2());
CurrentView = new View1();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Predicate<object> canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute(parameter);
}
public void Execute(object parameter)
{
execute(parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}
```
View1.xaml和View2.xaml可以是任何自定义的用户控件。在此示例中,它们非常简单,只包含一个TextBlock和一个Button:
View1.xaml:
```xaml
<UserControl x:Class="MultiViewSwitching.View1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid>
<TextBlock Text="View 1"/>
<Button Content="Switch to View 2" Click="SwitchToView2"/>
</Grid>
</UserControl>
```
View2.xaml:
```xaml
<UserControl x:Class="MultiViewSwitching.View2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid>
<TextBlock Text="View 2"/>
<Button Content="Switch to View 1" Click="SwitchToView1"/>
</Grid>
</UserControl>
```
最后,MainViewModel类中使用了一个简单的RelayCommand类来实现SwitchToView1Command和SwitchToView2Command的ICommand接口。这个RelayCommand类使用了委托来实现Execute和CanExecute方法。Execute方法将会调用传入的委托,而CanExecute方法将会检查传入的委托是否为null,或者如果不为null,它是否返回true。RaiseCanExecuteChanged方法用于在CanExecuteChanged事件发生时通知WPF重新评估ICommand的CanExecute方法。