使用按钮导航到导航窗口中的另一个页面
我正在尝试使用 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 留空。 我还尝试将绑定路径设置为导航窗口上各种与导航相关的公共属性。 到目前为止还没有任何效果。
我在这里缺少什么? 我真的不想用代码进行导航。
澄清。
如果我不使用按钮,而是使用超链接:
<Grid>
<TextBlock>
<Hyperlink
NavigateUri="Second.xaml">Go!
</Hyperlink>
</TextBlock>
</Grid>
一切都会按预期工作。 然而,我的 UI 要求意味着使用超链接是正确的。 我需要一个又大又胖的按钮供人们按下。 这就是为什么我想使用按钮进行导航。 我只是想知道如何让按钮提供与超链接在本例中相同的功能。
I'm trying to use the navigation command framework in WPF to navigate between Pages within a WPF application (desktop; not XBAP or 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:
<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. First.xaml is a Page. WPF automatically hosts my page in a NavigationWindow. Here's 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. The command is GoToPage, and the page is /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. I've also tried setting the Path to the Binding to various navigational-related public properties on the 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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据文档,仅
DocumentViewer
和FlowDocumentViewer
专门实现了这个命令。 您需要找到NavigationWindow
实现的导航命令,或者为此命令设置CommandBinding
并自行处理。According to the documentation, only
DocumentViewer
andFlowDocumentViewer
implement this command specifically. You'll need to either find a command for navigation thatNavigationWindow
implements, or set up aCommandBinding
for this command and handle it yourself.在 XAML 中:
在视图中(我们有一个包含所有这些内容的
Commands.cs
文件):在页面构造函数中,您可以连接两者:
NavigateHelpExecute 可以位于后面的代码中(这就是我们do),挂接到 ViewModel 事件处理程序,或者其他什么。 这样做的好处是您可以像这样禁用其他导航:
希望这有帮助。
In XAML:
In Views (We have a
Commands.cs
file that contains all of these):In the Page contstructor, you can connect the two:
NavigateHelpExecute can be in the code behind (which is what we do), hook into a ViewModel event handler, or whatever. The beauty of this is that you can disable other navigation like so:
Hope this helps.
您将需要使用
NavigationWindow
的NavigationService
,如下所示:XAML:
C#:
使用其中任何一个,您都可以使用
this
,我只为了清楚起见,在此处显示NavigationService
You will want to use the
NavigationService
of yourNavigationWindow
as follows:XAML:
C#:
With either of this you can use use
this
, I only show theNavigationService
here for clarity然后您可以将以下内容放入 xaml 中:
然后您仍然不需要页面后面的代码,并且您不需要向视图模型添加命令。
And then you can put the following in your 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.