Creating Pages or Windows in WPF

前端 未结 4 1846
别那么骄傲
别那么骄傲 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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:

    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.

提交回复
热议问题