简体   繁体   English

使用Button导航到NavigationWindow中的另一个页面

[英]Using a Button to navigate to another Page in a NavigationWindow

I'm trying to use the navigation command framework in WPF to navigate between Pages within a WPF application (desktop; not XBAP or Silverlight). 我正在尝试使用WPF中的导航命令框架在WPF应用程序(桌面;而不是XBAP或Silverlight)中的页面之间导航。

I believe I have everything configured correctly, yet its not working. 我相信我已经正确配置了一切,但它无法正常工作。 I build and run without errors, I'm not getting any binding errors in the Output window, but my navigation button is disabled. 我构建并运行没有错误,我没有在输出窗口中收到任何绑定错误,但我的导航按钮被禁用。

Here's the app.xaml for a sample app: 这是示例应用程序的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>

Note the StartupUri points to First.xaml. 注意StartupUri指向First.xaml。 First.xaml is a Page. First.xaml是一个页面。 WPF automatically hosts my page in a NavigationWindow. WPF自动在NavigationWindow中托管我的页面。 Here's First.xaml: 这是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>

The button's CommandTarget is set to the NavigationWindow. 按钮的CommandTarget设置为NavigationWindow。 The command is GoToPage, and the page is /Second.xaml. 命令是GoToPage,页面是/Second.xaml。 I've tried setting the CommandTarget to the containing Page, the CommandParameter to "Second.xaml" (First.xaml and Second.xaml are both in the root of the solution), and I've tried leaving the CommandTarget empty. 我已经尝试将CommandTarget设置为包含的Page,将CommandParameter设置为“Second.xaml”(First.xaml和Second.xaml都在解决方案的根目录中),并且我尝试将CommandTarget留空。 I've also tried setting the Path to the Binding to various navigational-related public properties on the NavigationWindow. 我还尝试在NavigationWindow上设置绑定路径到各种与导航相关的公共属性。 Nothing has worked so far. 到目前为止,没有任何工作。

What am I missing here? 我在这里错过了什么? I really don't want to do my navigation in code. 真的不想在代码中进行导航。


Clarification. 澄清。

If, instead of using a button, I use a Hyperlink: 如果我使用超链接而不是使用按钮:

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

everything works as expected. 一切都按预期工作。 However, my UI requirements means that using a Hyperlink is right out. 但是,我的UI要求意味着使用超链接是正确的。 I need a big fatty button for people to press. 我需要一个巨大的脂肪按钮供人们按。 That's why I want to use the button to navigate. 这就是我想使用按钮导航的原因。 I just want to know how I can get the Button to provide the same ability that the Hyperlink does in this case. 我只是想知道如何让Button提供与Hyperlink在这种情况下相同的能力。

In XAML: 在XAML中:

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

In Views (We have a Commands.cs file that contains all of these): 在视图中(我们有一个包含所有这些的Commands.cs文件):

public static RoutedCommand NavigateHelp = new RoutedCommand();

In the Page contstructor, you can connect the two: 在Page contstructor中,您可以连接两个:

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

NavigateHelpExecute can be in the code behind (which is what we do), hook into a ViewModel event handler, or whatever. NavigateHelpExecute可以在后面的代码中(这是我们所做的),挂钩到ViewModel事件处理程序,或者其他什么。 The beauty of this is that you can disable other navigation like so: 这样做的好处是你可以禁用其他导航:

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

Hope this helps. 希望这可以帮助。

According to the documentation , only DocumentViewer and FlowDocumentViewer implement this command specifically. 根据文档 ,只有DocumentViewerFlowDocumentViewer专门实现此命令。 You'll need to either find a command for navigation that NavigationWindow implements, or set up a CommandBinding for this command and handle it yourself. 您需要找到NavigationWindow实现的导航命令,或者为此命令设置CommandBinding并自行处理。

You will want to use the NavigationService of your NavigationWindow as follows: 您将需要使用NavigationServiceNavigationWindow ,如下所示:

XAML: XAML:

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

C#: C#:

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

With either of this you can use use this , I only show the NavigationService here for clarity 使用其中任何一个都可以使用this ,为了清楚起见,我只在此处显示NavigationService

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

And then you can put the following in your xaml: 然后你可以在你的xaml中加入以下内容:

<local:NavigateButton Content="NavigateButton" NavigateUri="Page2.xaml"/>

Then you still don't need code behind your pages, and you don't need to add commands to your viewmodel. 然后,您仍然不需要页面后面的代码,也不需要向viewmodel添加命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM