[英]How to navigate between multiple pages in one frame, in WPF C#
I have:我有:
Here is the frame in MainWindow.xaml:这是 MainWindow.xaml 中的框架:
<Frame x:Name="MainPage"
Content="{Binding ApplicationViewModel.CurentPage,
Source={x:Static viewMod:ViewModelLocator.Instanze},
Converter={local:ApplicationPageValueConverter}}"/>
ApplicationViewModel
is the application state as a view model: ApplicationViewModel
是应用程序 state 作为视图 model:
public class ApplicationViewModel
{
/// <summary>
/// The current page of the application
/// </summary>
public ApplicationPage CurentPage { get; set; } = ApplicationPage.Login;
}
ViewModelLocator
locates view models from the IoC
for use in binding in Xaml files ViewModelLocator
从IoC
定位视图模型以用于 Xaml 文件中的绑定
public class ViewModelLocator
{
/// <summary>
/// Singleton instance of the locator
/// </summary>
public static ViewModelLocator Instance { get; private set; } = new ViewModelLocator();
/// <summary>
/// The application view model
/// </summary>
public static ApplicationViewModel ApplicationViewModel => IoC.Get<ApplicationViewModel>();
}
In ApplicationPageValueConverter
I have this, to convert the page:在ApplicationPageValueConverter
我有这个,要转换页面:
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((ApplicationPage)value)
{
case ApplicationPage.Login:
return new LoginPage();
case ApplicationPage.SignUp:
return new SignUpPage();
default:
Debugger.Break();
return null;
}
}
In the MainViewModel
which is ViewModel
for MainWindow.xaml.cs I have a button "SignUp", and when I click the button is going to execute ICommand
whose is doing this:在MainViewModel
中,它是 MainWindow.xaml.cs 的ViewModel
,我有一个“SignUp”按钮,当我单击该按钮时,将执行执行此操作的ICommand
:
public ICommand LoginCommand { get; set; }
LoginCommand = new RelayCommand(() => Login());
private void Login()
{
IoC.Get<ApplicationViewModel>().CurentPage = ApplicationPage.SignUp;
}
The value of ApplicationViewModel.CurentPage
is changed to ApplicationPage.SignUp
but it doesn't go to ApplicationPageValueConverter
to convert/show the page. ApplicationViewModel.CurentPage
的值更改为ApplicationPage.SignUp
但它不会 go 到ApplicationPageValueConverter
来转换/显示页面。
Here is the IoC
code where OnStartup
I'm doing this:这是我正在执行此操作的OnStartup
的IoC
代码:
base.OnStartup(e);
IoC.SetUp();
....
I can't get whay it doesn't show the page, what I'm doing wrong?我不明白为什么它不显示页面,我做错了什么?
ApplicationViewModel
should implement INotifyPropertyChanged
and raise the PropertyChanged
event whenever the CurrentPage
property is set: ApplicationViewModel
应该实现INotifyPropertyChanged
并在设置CurrentPage
属性时引发PropertyChanged
事件:
public class ApplicationViewModel : INotifyPropertyChanged
{
private ApplicationPage _currentPage = ApplicationPage.Login;
/// <summary>
/// The current page of the application
/// </summary>
public ApplicationPage CurentPage
{
get { return _currentPage; }
set { _currentPage = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
This is required for the view to be notified of the change and the converter to get invoked again.这是通知视图更改和再次调用转换器所必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.