所以我试图在 visual studio 中制作一个 C# 程序,当单击 "next" 按钮时它会移动到一个新页面

So I am trying to make a C# program in visual studio which moves onto a new page when the "next" button is clicked

现在我有一个基本的程序。我才刚刚开始自学 C#,我不知道如何进入一个新页面,我可以在其中添加更多按钮和文本等。

这是我目前拥有的:

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;
        }
    }
}

这是我 运行 在 Raspberry Pi 3b 上的程序。到目前为止一切正常,但我想知道如何添加更多页面。将不胜感激。

此解决方案基于 WPF(Windows Presentation Foundation) C#。

假设您有 3 个页面需要导航,我们称它们为 Page1、Page2、Page3 在我们添加页面之前,让我们设置框架来容纳这些页面。

MainWindow.XAML

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

    </Grid>

代码背后:MainWindow.XAML.CS

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


      }

注意:因此,当您的应用 运行 出现时,您会在第 1 页上看到它 运行,并且在第 1 页上您有一个“下一步”按钮。 在定义这三个页面之前,我们需要从解决方案资源管理器 (Ctrl+Shift+A)

添加它们

代码隐藏: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));


      }
}

代码隐藏: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));


      }
  }

代码隐藏: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

      }
  }

输出:

页面已经按照我的要求进行了修改。但是上面的 属性 用于创建裸骨

互联网上有很多这方面的信息,我已经收藏了一些我最喜欢的,这应该可以帮助你做出决定。

但我建议您先熟悉 C#,然后再参考文档

参考文献:

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

https://docs.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?