简体   繁体   English

如何在一帧中的多个页面之间导航,在 WPF C#

[英]How to navigate between multiple pages in one frame, in WPF C#

I have:我有:

  • MainWindow.xaml ( where I have the frame ) MainWindow.xaml (我有框架
  • LoginPage.xaml登录页面.xaml
  • SignUpPage.xaml SignUpPage.xaml

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 ViewModelLocatorIoC定位视图模型以用于 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:这是我正在执行此操作的OnStartupIoC代码

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM