Here we add three interfaces for demonstration, MainWindow.xaml,,
1. Start the LoginView interface and click the button to jump to the HomeView interface.
2. Click the HomeView interface button to return to the LoginView interface
Two packages to be installed:
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="Login" 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="Back to login interface" 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()
{
();
}
}
Parent class:
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}}
Other functional categories
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>
///Number value
/// </summary>
Empty,
/// <summary>
/// Home page
/// </summary>
HomeView,
/// <summary>
/// Log in
/// </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;
}
}