簡體   English   中英

使用Button導航到NavigationWindow中的另一個頁面

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

我正在嘗試使用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設置為“Second.xaml”(First.xaml和Second.xaml都在解決方案的根目錄中),並且我嘗試將CommandTarget留空。 我還嘗試在NavigationWindow上設置綁定路徑到各種與導航相關的公共屬性。 到目前為止,沒有任何工作。

我在這里錯過了什么? 真的不想在代碼中進行導航。


澄清。

如果我使用超鏈接而不是使用按鈕:

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

一切都按預期工作。 但是,我的UI要求意味着使用超鏈接是正確的。 我需要一個巨大的脂肪按鈕供人們按。 這就是我想使用按鈕導航的原因。 我只是想知道如何讓Button提供與Hyperlink在這種情況下相同的能力。

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

希望這可以幫助。

根據文檔 ,只有DocumentViewerFlowDocumentViewer專門實現此命令。 您需要找到NavigationWindow實現的導航命令,或者為此命令設置CommandBinding並自行處理。

您將需要使用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

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM