How to navigate through windows with MVVM Light for WPF?

后端 未结 3 453
清歌不尽
清歌不尽 2020-12-12 23:03

I\'ve just started a new project in which the presentation layer will be done by WPF and MVVM Light by GalaSoft.

I need a lot of views and it\'s not clear to me how

3条回答
  •  忘掉有多难
    2020-12-12 23:29

    For a navigable application, you'll want your start up view to be a NavigationWindow instead of a Window

    
    

    Code behind:

    using System.Windows.Navigation;
    
    public partial class MainWindow : NavigationWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    

    The MVVM Light view templates use Window, but as you have guessed, you can just change it. If you want to be able to navigate to and from this view, make it a Page. This is how you navigate:

    
        
            
            

    Code Behind:

    using System.Windows;
    using System.Windows.Controls;
    
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // the Page class has a property "NavigationService" which allows you to navigate.
            // you can supply the "Navigate" method with a Uri or an object instance of the page 
            base.NavigationService.Navigate(new Page2());
        }
    }
    

提交回复
热议问题