这里一共添加三个界面进行演示,MainWindow.xaml,,
1,启动显示LoginView界面,点击按钮跳转到HomeView界面
2,点击HomeView界面按钮返回LoginView界面
需要安装的两个包:
1,
2,CommunityToolkit.Mvvm
<Window x:Class=""
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:d="/expression/blend/2008"
xmlns:mc="/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" Content="{Binding CurrentPage,Converter={local:ApplicationPageValueConverter}}" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainViewModel MainViewModel;
public MainWindow()
{
InitializeComponent();
MainViewModel = new MainViewModel();
= MainViewModel;
+= MainWindow_Loaded;
}private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
();
}
}
public partial class MainViewModel:BaseViewModel
{
public ApplicationPage CurrentPage { get; set; } = ;
}
<Page x:Class=""
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:mc="/markup-compatibility/2006"
xmlns:d="/expression/blend/2008"
xmlns:local="clr-namespace:"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="LoginView"><Grid>
<Button Content="登录" Height="100" Width="100" Command="{Binding LoginCommand}"/>
</Grid>
</Page>
public partial class LoginView : Page
{
public LoginViewModel LoginViewModel;
public LoginView()
{
InitializeComponent();
LoginViewModel = new LoginViewModel();
= LoginViewModel;
}
}
using ;
using System.Windows.Input;namespace WpfApp5
{
public class LoginViewModel : BaseViewModel
{
public ICommand LoginCommand { get; set; }public LoginViewModel()
{
LoginCommand = new RelayCommand(LoginCommandEvent);
}private void LoginCommandEvent()
{
();
}
}
}
<Page x:Class=""
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:mc="/markup-compatibility/2006"
xmlns:d="/expression/blend/2008"
xmlns:local="clr-namespace:"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="HomeView"><Grid>
<Button Content="返回登录界面" Height="100" Width="100" Command="{Binding HomeCommand}"/>
</Grid>
</Page>
public partial class HomeView : Page
{
public HomeViewModel homeViewModel;
public HomeView()
{
InitializeComponent();
homeViewModel = new HomeViewModel();
= homeViewModel;
}
}
public class HomeViewModel : BaseViewModel
{
public ICommand HomeCommand { get; set; }public HomeViewModel()
{
HomeCommand = new RelayCommand(HomeCommandEvent);
}private void HomeCommandEvent()
{
();
}
}
父类:
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}}
其他功能类
public class ApplicationPageValueConverter : BaseValueConverter<ApplicationPageValueConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((ApplicationPage)value)
{
case :
return new TextBlock { Text = "404 Not Found" };
case :
return new LoginView();
case :
return new HomeView();
default:
throw new ArgumentException("Invalid value passed to ApplicationPageViewConverter");
}
}public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public abstract class BaseValueConverter<T> : MarkupExtension, IValueConverter where T : class, new()
{
#region Private Members
/// <summary>
/// A single static instance of this value converter
/// </summary>
private static T mConvert = null;
#endregionpublic override object ProvideValue(IServiceProvider serviceProvider)
{
return mConvert ?? (mConvert = new T());
}#region Value Converter Methods
/// <summary>
/// The method to convert one type another
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);/// <summary>
/// The method to convert a value back to it's source type
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
#endregion
}
public enum ApplicationPage
{
/// <summary>
/// 空值
/// </summary>
Empty,
/// <summary>
/// 主页
/// </summary>
HomeView,
/// <summary>
/// 登录
/// </summary>
LoginView,}
class NavigationPateService
{
public static NavigationPateService Instance { get; } = new NavigationPateService();private MainViewModel mainVM;
public void Navigate(ApplicationPage page)
{
if (mainVM == null)
{
mainVM = (MainViewModel);
}= page;
}
}