【问题标题】:Using a Button to navigate to another Page in a NavigationWindow使用按钮导航到 NavigationWindow 中的另一个页面
【发布时间】:2009-04-29 19:36:05
【问题描述】:

我正在尝试使用 WPF 中的导航命令框架在 WPF 应用程序(桌面;不是 XBAP 或 Silverlight)内的页面之间导航。

我相信我已经正确配置了所有内容,但它无法正常工作。我构建并运行没有错误,我在输出窗口中没有收到任何绑定错误,但我的导航按钮被禁用。

这是示例应用的 app.xaml:

<Application x:Class="Navigation.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="First.xaml">
</Application>

注意 StartupUri 指向 First.xaml。 First.xaml 是一个页面。 WPF 会自动在 NavigationWindow 中托管我的页面。这是 First.xaml:

<Page x:Class="Navigation.First"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="First">
    <Grid>
        <Button 
            CommandParameter="/Second.xaml" 
            CommandTarget="{Binding RelativeSource=
                {RelativeSource 
                    FindAncestor, 
                    AncestorType={x:Type NavigationWindow}}}" 
            Command="NavigationCommands.GoToPage" 
            Content="Go!"/>
    </Grid>
</Page>

按钮的 CommandTarget 设置为 NavigationWindow。命令是 GoToPage,页面是 /Second.xaml。我尝试将 CommandTarget 设置为包含页面,将 CommandParameter 设置为“Second.xaml”(First.xaml 和 Second.xaml 都在解决方案的根目录中),并且我尝试将 CommandTarget 留空。我还尝试将绑定路径设置为 NavigationWindow 上与导航相关的各种公共属性。到目前为止没有任何效果。

我在这里缺少什么?我真的不想在代码中进行导航。


澄清。

如果我不使用按钮,而是使用超链接:

<Grid>
    <TextBlock>
       <Hyperlink 
           NavigateUri="Second.xaml">Go!
       </Hyperlink>
    </TextBlock>
</Grid>

一切都按预期进行。但是,我的 UI 要求意味着使用超链接是正确的。我需要一个胖乎乎的大按钮供人们按下。这就是我想使用按钮导航的原因。我只是想知道在这种情况下如何让 Button 提供与 Hyperlink 相同的功能。

【问题讨论】:

    标签: wpf button navigationwindow


    【解决方案1】:

    根据documentation,只有DocumentViewerFlowDocumentViewer具体实现了这个命令。您需要找到NavigationWindow 实现的导航命令,或者为此命令设置CommandBinding 并自行处理。

    【讨论】:

    • 谢谢,但这并不完全适用于这种情况。我已经编辑了我的问题,希望能澄清它。我正在尝试为按钮找到一种方法来命令 NavigationWindow 下一步要去哪里。我的代码不起作用,所以我不坚持要那样做。我正在寻找一种方法来让按钮与 NavigationWindow 一起使用。
    • 基本上这是唯一的方法。我在我的虚拟机中使用命令来连接按钮。基本视图模型包含对主窗口导航服务的引用。
    【解决方案2】:

    在 XAML 中:

    <Button Command="{x:Static Views:Commands.NavigateHelp}" Content="Help"/>
    

    在视图中(我们有一个包含所有这些的 Commands.cs 文件):

    public static RoutedCommand NavigateHelp = new RoutedCommand();
    

    在Page构造器中,可以将两者连接起来:

    CommandBindings.Add(new CommandBinding(Commands.NavigateHelp, NavigateHelpExecute));
    

    NavigateHelpExecute 可以在后面的代码中(这就是我们所做的)、挂钩到 ViewModel 事件处理程序或其他任何东西。这样做的好处是您可以像这样禁用其他导航:

    CommandBindings.Add(new CommandBinding(NavigationCommands.Refresh, null));
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您需要使用NavigationServiceNavigationWindow,如下所示:

      XAML:

          <Button HorizontalAlignment="Right" Name="continueButton" Width="75" Margin="0,0,8,11" Height="23" VerticalAlignment="Bottom" Click="continueButton_Click">
              Continue
          </Button>
      

      C#:

          private void continueButton_Click(object sender, RoutedEventArgs e)
          {
              this.NavigationService.GoForward();
              //or
              this.NavigationService.Navigate("Second.xaml")
          }
      

      你可以使用this,为了清楚起见,我在这里只显示NavigationService

      【讨论】:

      • 这可行,但对于 MVVM,命令模式更可取。感谢您的回答。
      【解决方案4】:
      public class NavigateButton : Button
      {
          public Uri NavigateUri { get; set; }
      
          public NavigateButton()
          {
              Click += NavigateButton_Click;
          }
      
          void NavigateButton_Click(object sender, System.Windows.RoutedEventArgs e)
          {
              var navigationService = NavigationService.GetNavigationService(this);
              if (navigationService != null)
                  navigationService.Navigate(NavigateUri);
          }
      }
      

      然后您可以将以下内容放入您的 xaml:

      &lt;local:NavigateButton Content="NavigateButton" NavigateUri="Page2.xaml"/&gt;

      那么您仍然不需要页面后面的代码,也不需要向视图模型添加命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-01
        • 2021-08-19
        • 2023-03-21
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多