Creating Pages or Windows in WPF

前端 未结 4 1848
别那么骄傲
别那么骄傲 2021-02-20 13:46

I\'m new to using WPF. I have the following program I want to create: -Application opens up with one button -User clicks button and it takes them to a new page with various inpu

相关标签:
4条回答
  • 2021-02-20 14:23

    Initially there doesn't seem to be much of a difference in the preference of what should be used: Pages or Windows. However, looking at the intended goal of the application, I would suggest using UserControls instead of Pages, as Pages seem to focus on Web related content, though they can be used in stand alone applications as well. Another argument that has been made in another post is referring to the MSDN-documentation, and points out that in using a Page the NavigationWindow that it is hosted in does not remember the instance of the content that is navigated to and thus other WPF-techniques are needed to store that content in your navigation history.

    NavigationWindow does not store an instance of a content object in navigation history. Instead, NavigationWindow creates a new instance of the content object each time it is navigated to by using navigation history. This behavior is designed to avoid excessive memory consumption when large numbers and large pieces of content are being navigated to. Consequently, the state of the content is not remembered from one navigation to the next. However, WPF provides several techniques by which you can store a piece of state for a piece of content in navigation history.

    If you use a UserControl, you wouldn't have that problem if your goal is to create a native application anyway. You can download this template as an example to use UserControls instead.

    0 讨论(0)
  • 2021-02-20 14:37

    You can change your application object's MainWindow reference to another Window object.

    Application.Current.MainWindow = new SecondWindowToBeDisplayed();
    
    0 讨论(0)
  • 2021-02-20 14:40

    The use of the NavigationService is the right way to do that. You have to add a frame to your windows to show your pages, then navigating between them with the NavigationService.

    0 讨论(0)
  • 2021-02-20 14:46

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

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

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

    and this in your Page1.xaml.cs:

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

    Alternatively you could use this:

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

    Generally it is easier to do the first, because you can also set properties of Page2. For example, if Page2 has a public "CurrentItem" property you could say:

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

    You can't do that with the Uri-based syntax.

    You can also create instances of various pages (Page1, Page2, etc) and store them in your Application object, then switch to them like this:

      NavigationSerivce.Navigate(App.Page2);
    

    This way if you ever navigate to Page2 later you will get exactly the same Page2 object. Alternatively you could use NavigationService's Journaling feature to help with this.

    0 讨论(0)
提交回复
热议问题