【问题标题】:How to navigate to other page with button in WPF如何使用 WPF 中的按钮导航到其他页面
【发布时间】:2014-01-14 06:34:40
【问题描述】:

我在 Page2.xaml 的名称下设置了第二个 .xaml 页面,我希望这样当我的按钮被点击时,用户会被带到 Page2.xaml

我的 Page1.xaml 内的按钮有这个:

<Grid>
    <Button x:Name="localModeBtn" 
            Style="{StaticResource MainButtonStyle}"  
            Content="local mode" 
            Click="localModeBtn_Click" />
</Grid>

对于按钮事件处理程序:

private void localModeBtn_Click(object sender, RoutedEventArgs e)
    {
        Uri uri = new Uri("Page2.xaml", UriKind.Relative);
        this.NavigationService.Navigate(uri);
    }

点击按钮后,我收到一条错误消息,提示“找不到资源 page2.xaml” 问题是 Page2.xamlPag1.xaml 位于同一文件夹中,所以我看不出哪里出错了?

【问题讨论】:

标签: c# wpf xaml button


【解决方案1】:

使用任何容器并将内容绑定到视图模型或代码隐藏中的任何属性。 之后,您只需通过设置新页面并调用 PropertyChanged 事件来更新属性(请参阅 INotifyPropertyChanged 接口)。这将更新容器的内容,您可以显示任何您想要的内容。

【讨论】:

    【解决方案2】:

    如果你想要一个单独的窗口

    NavigationWindow navWIN = new NavigationWindow();
    navWIN.Content = new pageWFbchAdmin();
    navWIN.Show(); 
    //winBchAdmin.ShowDialog();
    

    【讨论】:

      【解决方案3】:

      我自己的问题的解决方案:

      我觉得为自己的问题提供解决方案有点愚蠢,但感谢 Jasti 的 link,我能够整理出我的代码。由于他只发表了评论,我无法将其标记为答案,所以这里是解决方案。

      我将 NavigationWindow 更改为 Window 并插入:

      <DockPanel>
          <Frame x:Name="_NavigationFrame" NavigationUIVisibility="Hidden" />
      </DockPanel>
      

      在我添加的 MainWindow.xaml.cs 的构造函数中:

      _NavigationFrame.Navigate(new Page1());
      

      那么最后一步就是将按钮事件处理程序调整为:

      this.NavigationService.Navigate(new Uri("Pages/Page2.xaml", UriKind.Relative));
      

      【讨论】:

        【解决方案4】:

        您不需要任何 C# 代码,只需在 XML 中完成:

        <Button Content="local mode"
            Command="NavigationCommands.GoToPage"
            CommandParameter="/Page2.xaml"/>
        

        (未测试重新格式化的代码)

        【讨论】:

        • 当我尝试应用Command="NavigationCommands.GoToPage"时,我的按钮被禁用了@
        【解决方案5】:
        private void Navigate_Click(object sender, RoutedEventArgs e)//By Prince Jain 
        {
            this.NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative));
        }
        

        【讨论】:

          【解决方案6】:

          你应该使用这个,这适用于me

          var Page2= new Page2(); //create your new form.
          Page2.Show(); //show the new form.
          this.Close(); //only if you want to close the current form.
          

          您的解决方案中有一个带有 page.xaml 正确名称的页面的 variable type。 之后,你应该使用它的方法来完成它。

          【讨论】:

          • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
          • 同意!我需要一个简短的描述。
          【解决方案7】:

          我的解决方案是在主窗口中添加一个框架MainWindow.xaml

          <Frame Name="Main" Content="" Margin="127,0,0,0" Background="#FFFFEDED" />
          

          导航:

          1- 在按钮单击时从主窗口导航:

          private void Button_Click(object sender, RoutedEventArgs e)
          {
             // navigate to pages/projects.xaml inside the main frame
             Main.Content = new MyProject.Pages.Projects();
          }
          

          2- 如果从框架内的页面导航,例如 Projects.xaml

          // declare a extension method in a static class (its your choice if you want to reuse)
          // name the class PageExtensions (you can choose any name)
          
          namespace MyProject
          {
              public static class PageExtensions
              {
                  public static void NavigateTo(this Page page, string path)
                  {
                      Frame pageFrame = null;
                      DependencyObject currParent = VisualTreeHelper.GetParent(page);
          
                      while (currParent != null && pageFrame == null)
                      {
                          pageFrame = currParent as Frame;
                          currParent = VisualTreeHelper.GetParent(currParent);
                      }
          
                      if (pageFrame != null)
                      {
                          pageFrame.Source = new Uri(path, UriKind.Relative);
                      }
                  }
              }
          }
          
          
          // to navigate from 'pages/projects.xaml' to another page
          // heres how to call the extension on button click
          
          this.NavigateTo("NewProject.xaml");
          

          此外,您可以添加另一个扩展方法,该方法需要另一个 Page 对象,以防您想将参数传递给构造函数

          // overloading NavigateTo
          public static void NavigateTo(this Page page, Page anotherPage)
          {
              Frame pageFrame = null;
              DependencyObject currParent = VisualTreeHelper.GetParent(page);
          
              while (currParent != null && pageFrame == null)
              {
                  pageFrame = currParent as Frame;
                  currParent = VisualTreeHelper.GetParent(currParent);
              }
          
              // Change the page of the frame.
              if (pageFrame != null)
              {
                  pageFrame.Navigate(anotherPage);
              }
          }
          
          // usage
          this.NavigateTo(new Pages.EditProject(id));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-12-24
            • 2023-01-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-25
            • 2021-06-29
            相关资源
            最近更新 更多