0

I have a simple WPF app (code-behind) and would like to navigate from one view to another in code-behind.

In UWP, I could do this

NavigationService.Navigate(typeof(destinationView), "myParam");

since the NavigationService was a public static class.

Could someone explain how this can be done with the WPF project?

Template Studio Settings

<genTemplate:Item Name="generator" Value="Template Studio"/>
<genTemplate:Item Name="wizardVersion" Version="v5.1" />
<genTemplate:Item Name="projectType" Value="SplitView" />
<genTemplate:Item Name="framework" Value="CodeBehind" />
<genTemplate:Item Name="platform" Value="Wpf" />

Thank you.

Etheraex
  • 508
  • 6
  • 17
cdt
  • 5
  • 3

1 Answers1

0

Could someone explain how this can be done with the WPF project?

Just inject your view(s) with an INavigationService:

public partial class SomePage : Page
{
    private readonly INavigationService _navigationService
    public MainPage(INavigationService navigationService)
    {
        InitializeComponent();
        _navigationService = navigationService;
    }
    ...
}

Look at the generated MainPage.xaml.cs class for yet an example.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • One related question - why didn't I need to make changes to the code that creates the view; I assumed I would've needed to add an argument since the constructor has the (INavigationService navigationService) parameter. – cdt Jun 08 '22 at 16:25