6

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 を含むページに、CommandParameter を「Second.xaml」に設定しようとしました (First.xaml と Second.xaml はどちらもソリューションのルートにあります)。CommandTarget を空のままにしてみました。また、Path を Binding に、NavigationWindow のさまざまなナビゲーション関連のパブリック プロパティに設定しようとしました。これまでのところ何も機能していません。

ここで何が欠けていますか?コードでナビゲーションを行いたくありません


説明。

ボタンを使用する代わりに、ハイパーリンクを使用する場合:

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

すべてが期待どおりに機能します。ただし、私の UI 要件は、ハイパーリンクを使用することが適切であることを意味します。人々が押すための大きな太ったボタンが必要です。そのため、ボタンを使用してナビゲートしたいと考えています。この場合、ハイパーリンクと同じ機能をボタンに提供する方法を知りたいだけです。

4

4 に答える 4

4

XAMLの場合:

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

ビュー内(Commands.csこれらすべてを含むファイルがあります):

public static RoutedCommand NavigateHelp = new RoutedCommand();

ページコンストラクタでは、次の2つを接続できます。

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

NavigationHelpExecuteは、コードビハインド(これが私たちが行うことです)に含めることも、ViewModelイベントハンドラーにフックすることもできます。これの利点は、次のように他のナビゲーションを無効にできることです。

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

お役に立てれば。

于 2010-05-07T19:32:42.603 に答える
4

ドキュメントによると、このコマンドのみDocumentViewerを具体的に実装してください。FlowDocumentViewer実装するナビゲーション用のコマンドを見つけるか、このコマンド用に をNavigationWindow設定しCommandBindingて自分で処理する必要があります。

于 2009-04-29T19:44:19.020 に答える
2

を次のように使用しNavigationServiceます。NavigationWindow

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

これのどちらでも使用できます use thisNavigationServiceここではわかりやすくするためにのみ示します

于 2009-07-28T22:16:32.927 に答える
0
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 にコマンドを追加する必要もありません。

于 2015-03-16T13:55:41.737 に答える