ボタンを使用してNavigationWindowの別のページに移動する
-
03-07-2019 - |
質問
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を<!> quot; Second.xaml <!> quot;に設定しようとしました。 (First.xamlとSecond.xamlは両方ともソリューションのルートにあります)、CommandTargetを空のままにしてみました。また、NavigationWindowのさまざまなナビゲーション関連のパブリックプロパティへのBindingへのパスを設定しようとしました。今のところ何も機能していません。
ここで何が欠けていますか?コードでナビゲーションを行いたくない 。
明確化。
ボタンを使用する代わりに、ハイパーリンクを使用する場合:
<Grid>
<TextBlock>
<Hyperlink
NavigateUri="Second.xaml">Go!
</Hyperlink>
</TextBlock>
</Grid>
すべてが期待どおりに機能します。ただし、私のUI要件は、ハイパーリンクを使用するのが正しいことを意味します。人々が押すための大きな太ったボタンが必要です。そのため、ボタンを使用してナビゲートする必要があります。この場合、ハイパーリンクと同じ機能を提供するボタンを取得する方法を知りたいだけです。
解決
ドキュメント、このコマンドを具体的に実装するのはDocumentViewer
およびFlowDocumentViewer
のみです。 NavigationWindow
が実装するナビゲーション用のコマンドを見つけるか、このコマンド用にCommandBinding
を設定して自分で処理する必要があります。
他のヒント
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));
NavigateHelpExecuteは、コードビハインド(これは私たちが行うことです)、ViewModelイベントハンドラーなどにフックすることができます。この利点は、次のように他のナビゲーションを無効にできることです:
CommandBindings.Add(new CommandBinding(NavigationCommands.Refresh, null));
これがお役に立てば幸いです。
次のように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")
}
どちらの場合でも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"/>
その後、ページの背後にコードを必要とせず、ビューモデルにコマンドを追加する必要はありません。