我正在尝试使用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设置为包含的Page,CommandParameter设置为<!> quot; Second.xaml <!> quot; (First.xaml和Second.xaml都在解决方案的根目录中),我尝试将CommandTarget留空。我还尝试在NavigationWindow上设置绑定路径到各种与导航相关的公共属性。到目前为止,没有任何工作。

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


澄清。

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

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

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

有帮助吗?

解决方案

根据文档,只有DocumentViewerFlowDocumentViewer专门实现此命令。您需要找到NavigationWindow实现的导航命令,或者为此命令设置CommandBinding并自行处理。

其他提示

在XAML中:

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

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

public static RoutedCommand NavigateHelp = new RoutedCommand();

在Page contstructor中,您可以连接两个:

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

NavigateHelpExecute可以在后面的代码中(这就是我们所做的),挂钩到ViewModel事件处理程序,或者其他什么。这样做的好处是你可以禁用其他类似的导航:

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

希望这有帮助。

您需要使用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,我只在这里显示<=>以获得清晰度

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中添加以下内容:

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

然后,您仍然不需要在页面后面添加代码,也不需要向viewmodel添加命令。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top