我正在尝试使用 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 自动将我的页面托管在导航窗口中。这是 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 留空。我还尝试将绑定路径设置为导航窗口上各种与导航相关的公共属性。到目前为止还没有任何效果。
我在这里缺少什么?我really不想用代码进行导航。
澄清。
如果我不使用按钮,而是使用超链接:
<Grid>
<TextBlock>
<Hyperlink
NavigateUri="Second.xaml">Go!
</Hyperlink>
</TextBlock>
</Grid>
一切都按预期进行。然而,我的 UI 要求意味着使用超链接是正确的。我需要一个又大又胖的按钮供人们按下。这就是为什么我想使用按钮进行导航。我只是想知道如何让按钮提供与超链接在本例中相同的功能。