Navigation ベースの WPF アプリケーションでも同様の設計上の問題がありました。私の解決策は、ViewModel で Action コマンドを定義し、View で Navigation コマンドを定義するか、すべてをホストする NavigationWindow でグローバル ナビゲーションをコマンドとして定義することでした。
インタラクションがアクションとナビゲーションの両方をもたらす場合など、場合によっては、View レベルのコマンドで ViewModel コマンドを呼び出してから、Navigating でアクションを完了させます。
この方法に完全に満足しているかどうかはわかりませんが、目標の達成には成功していますが、間違っていると感じています. あなたがより良い解決策を見つけたら、私は非常に興味があります
ナビゲーションにつながるボタンの例
<Button Style="{StaticResource HyperLinkButtonStyle}"
Command="{Binding GoCurrentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=View:MainWindow, AncestorLevel=1}}"
Content="Details"/>
アクション付き通常ボタン
<Button
Style="{StaticResource HyperLinkButtonStyle}"
Command="{Binding CompleteCommand}"
Content="Complete"/>
ViewModel への委任を伴うビュー レベル コマンドの例を次に示します (ReactiveUI の使用に注意してください。そのため、構文は慣れ親しんだものとは異なる場合があります)。
public class MainWindow : NavigationWindow
{
...
//This defines when the command can be run
this._completeCommand = new ReactiveCommand(((SolutionViewModel)DataContext).CompleteCommand.CanExecuteObservable);
//This defines what to do when the command is run
this._completeCommand.Subscribe(
_ =>
{
((SolutionViewModel)DataContext).CompleteCommand.Execute(null);
this.NavigationService.Navigate(new IntentionsListPage { DataContext = this.DataContext });
});