所以我试图在visual studio中制作一个C#程序,当#" next"单击按钮

时间:2018-05-08 18:15:32

标签: c# visual-studio raspberry-pi3

现在我有一个基本程序。我刚刚开始自学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上运行的程序。到目前为止它一切正常但我想知道如何添加更多页面。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

此解决方案基于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);


      }

注意:因此,当您的应用程序运行时,您将看到它从Page1和第1页上运行,您有一个下一步按钮。 在我们定义三个页面之前,我们需要从Solution explorer(Ctrl + Shift + A)

添加它们

enter image description here

代码背后: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

      }
  }

输出

根据我的要求修改了页面。但上面的属性用于创建裸骨

enter image description here

互联网上有大量的信息,我已经为我的一些收藏书添加了书签,这可以帮助你做出决定。

但我建议您更熟悉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?