Create pages or Windows in WPF

I am new to using WPF. I have the following program that I want to create: -An application opens with one button -Uses a click button, and it transfers them to a new page with different input.

I am confused by how I need to do this. I tried to open a new window, but I do not want the window to open, I want it to be one window. I tried to create a new page and navigate to it using NavigationService, but could not get it to work.

Basically, I want to create a workflow when a user enters some things, clicks the next button and takes to a new page to enter additional information. Can someone point me in the right direction?

+6
wpf
source share
4 answers

Use the pages in your application and use the NavigationService to switch between them.

For example, if you have two pages in your folder, "Page1" and "Page2", you can include the following in Page1.xaml:

<Button Content="Next" Click="NextClicked" /> 

and this is in your Page1.xaml.cs file:

 void NextClicked(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Page2()); } 

Alternatively you can use this:

  NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative)); 

It is usually easier to do the first, because you can also set the page properties. For example, if Page2 has the public property "CurrentItem", you can say:

  NavigationService.Navigate(new Page2 { CurrentItem = this.Something }); 

You cannot do this with Uri-based syntax.

You can also create instances of different pages (Page1, Page2, etc.) and save them in your application, and then switch to them as follows:

  NavigationSerivce.Navigate(App.Page2); 

That way, if you ever get to the page later, you get exactly the same Page2 object. Alternatively, you can use the NavigationService Journaling function to help with this.

+7
source share

Initially, there does not seem to be much of a difference in the preference for what to use: Pages or Windows. However, looking at the intended purpose of the application, I would suggest using UserControls instead of Pages, since the pages seem to focus on web content, although they can be used in stand-alone applications. Another argument that was made in another post relates to the MSDN documentation , and indicates that when you use the NavigationWindow page in which it is placed, the copy of the content that you are navigating to is not remembered, and therefore, to store it in the navigation history need other WPF methods.

NavigationWindow does not store an instance of the content object in the navigation history. Instead, the NavigationWindow creates a new instance of the content object each time it is moved using the navigation history. This behavior prevents excessive memory consumption when navigating large quantities and large pieces of content. Therefore, the state of the content is not remembered from one navigation to another. However, WPF does provide several methods by which you can store part of the state for part of the content in the navigation history.

If you use UserControl, you would not have this problem if your goal is to create your own application. You can download this template as an example for using UserControls.

+2
source share

Using NavigationService is the right way to do this. You must add a frame to your windows to display your pages and then navigate between them using the NavigationService.

+1
source share

You can change the MainWindow application object to another Window object.

 Application.Current.MainWindow = new SecondWindowToBeDisplayed(); 
0
source share

All Articles