WPF按钮单击引向页面

时间:2018-09-20 00:24:57

标签: c# html wpf

我目前在主窗口中有3个按钮。按钮1单击后,我想转到Page1.xaml。按钮2我想转到Page2.xaml等。我该怎么办?

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {


    }
}



}

1 个答案:

答案 0 :(得分:0)

我希望下面的代码会有所帮助:

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page1.xaml", UriKind.Relative);
      window.Show();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page2.xaml", UriKind.Relative);
      window.Show();
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
      NavigationWindow window = new NavigationWindow();
      window.Source = new Uri("Page3.xaml", UriKind.Relative);
      window.Show();
    }
}
}

您可以通过将NavigationWindow声明为全局实例并在构造函数中进行初始化来进一步优化,但这取决于您。