0

Right now I have a basic program. I've only just started teaching myself C# and I don't know how to move onto a new page on which I can add more buttons and text etc.

This is what I have so far:

namespace HelloWorld
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Next.Visibility = Visibility.Collapsed;
        }

        private void ClickMe_Click(object sender, RoutedEventArgs e)
        {
            this.HelloMessage.Text = "This is a test message deployed from Rohan's PC.";
            this.ClickMe.Visibility = Visibility.Collapsed;
            this.Next.Visibility = Visibility.Visible;
        }
        private void Next_Click(object sender, RoutedEventArgs e)
        {
            this.Next.Visibility = Visibility.Collapsed;
            this.HelloMessage.Visibility = Visibility.Collapsed;
        }
    }
}

This is a program which I am running on my Raspberry Pi 3b. So far it all works but I would like to know how to add more pages. Help would be much appreciated.

johnny 5
  • 19,893
  • 50
  • 121
  • 195
4RJ
  • 125
  • 2
  • 8
  • [This tutorial](https://learn.microsoft.com/en-us/windows/uwp/design/basics/navigate-between-two-pages) can be a starting point (Googled 'UWP page navigation', 1st entry from Microsoft) – Ivan García Topete May 08 '18 at 18:32
  • [Navication basics for UWP](https://learn.microsoft.com/en-us/windows/uwp/design/basics/navigation-basics) also from Microsoft – Ivan García Topete May 08 '18 at 18:34
  • You can use page navigation in WPF using frames, does a WPF solution work for you ? – Clint May 08 '18 at 18:42
  • I don't think I'll be using WPF. For now I'll just use new pages connected with Response.Redirect(). Thanks though. – 4RJ May 08 '18 at 18:50

1 Answers1

0

This solution is based on WPF(Windows Presentation Foundation) C#.

Assuming that you have 3 pages that you need to navigate lets call them Page1, Page2, Page3 Before we add pages lets set up our frame to hold these pages.

MainWindow.XAML

    <Grid>
        <Frame x:Name="WizardWindowFrame" Content="" NavigationUIVisibility="Hidden" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

    </Grid>

Code Behind: MainWindow.XAML.CS

public MainWindow()
        {
            InitializeComponent();
         Title title = new Title(); // Navigate to Page1
         WizardWindowFrame.NavigationService.Navigate(title);


      }

Note: So when your app runs you will see it run from Page1 and on Page 1 you have a Next Button. Before we define the three pages we need to add them from Solution explorer (Ctrl+Shift+A)

enter image description here

Code Behind: Title.XAML.CS

   public partial class Title : Page
   {
      public Title()
      {
         InitializeComponent();

      }
public void TitleButtonNext_Click(object sender, EventArgs e)
      {


       Middle middle = new Middle(); // Navigate to Page 2 on click
       this.NavigationService.Navigate(new Uri("Middle.xaml", UriKind.Relative));


      }
}

Code Behind: Middle.XAML.CS

 public partial class Middle: Page
   {
      public Middle()
      {
         InitializeComponent();

      }
public void MiddleButtonNext_Click(object sender, EventArgs e)
      {


       Final final = new Final(); // Navigate to Page 3 on click
       this.NavigationService.Navigate(new Uri("Final.xaml", UriKind.Relative));


      }
  }

Code Behind: Final.XAML.CS

 public partial class Final: Page
   {
      public Final()
      {
         InitializeComponent();

      }
public void FinishButtonBack_Click(object sender, EventArgs e)
      {

      Middle middle = new Middle(); 
      this.NavigationService.Navigate(middle); //Goes to the previous page

      }
  }

Output:

Pages have been modified according to my requirements. But the above property was used to create the bare bone

enter image description here

There is a ton of information in the internet for this I have bookmarked some of my favorites, which should help you decide.

But I would recommend that you get more comfortable with C# then refer the docs

References:

https://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/

https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/navigation-topologies-overview#Navigation_over_a_Fixed_Linear_Topology

Window vs Page vs UserControl for WPF navigation?

Clint
  • 6,011
  • 1
  • 21
  • 28
  • That's quite a lot of code and I'm not sure if I'm that confident with C# yet xD. Thanks though, I'll look into it. – 4RJ May 08 '18 at 19:29
  • Not really, You have a MainWindow which holds the frame and all we're doing is navigating from Page 1 > Page 2 > Page 3. – Clint May 08 '18 at 19:31
  • But like I said earlier it is based on WPF. If you're objective is to build a simple navigation service, its pretty straightforward. Another option you have is to use MVVM which belongs to the MVC family – Clint May 08 '18 at 19:36
  • Ok thanks I've just read through it all and I'll give it a go. – 4RJ May 09 '18 at 15:42
  • Oops. Actually, I don't have the WPF page. I'm not sure why but I believe it is because of how I made the file. I am using Windows Universal and not Visual C# – 4RJ May 09 '18 at 15:43
  • yes that's right, if you want to build the page navigation then I think you can go WPF Project. I have build one already, may be I can help! – Clint May 09 '18 at 15:55
  • I think I've figured it out now. But thank's anyways. – 4RJ May 10 '18 at 15:29
  • @Danger, Great, so did you chose the WPF approach ? – Clint May 10 '18 at 15:55