Navigation and Routing

From truxwiki.com
Jump to navigation Jump to search

This page is intended to document the Navigation and Routing of the WPF applilcation, or the way different Pages and Windows are opened and closed.

UI Guide Menu

  1. Entry Point
  2. Navigation and Routing
  3. Databinding
  4. Dependency Injection
  5. Task Bar Icon
  6. Truxton CLI options
  7. How Reports are Displayed
  8. How Dialog Forms Work
  9. Forms with Images
  10. Images
  11. Bookmarks

Main Resources

Resource Summary Path
PageManager.cs This class manages the navigation tools such as : page loading, the bread crumbs, last page loaded, \Truxton\Client\TruxtonClient\TruxtonClient\PresentationCore\PageManager.cs
HomeViewModel.cs Adds the NavItems to the Main Menu \Truxton\Client\TruxtonClient\TruxtonClient\ViewModels\HomeViewModel.cs
ViewModelRegistrar.cs Registers the ViewModels so that the WorkspaceFactory can associate the View with the ViewModel \Truxton\Client\TruxtonClient\TruxtonClient\PresentationCore\ViewModelRegistrar.cs
View Directory Contains the various Views \Truxton\Client\TruxtonClient\TruxtonClient\Views\
Templates Directory Contains Templates which are additional XAML that may be used in each Views \Truxton\Client\TruxtonClient\TruxtonClient\Templates\
ViewModel Directory Contains the various ViewModels for each view \Truxton\Client\TruxtonClient\TruxtonClient\ViewModels\


Adding a New "Page"

This is an example of adding a new page in order to understand what is necessary.
  1. Create the View
  2. Create the ViewModel
  3. Register the ViewModel in the ViewModelRegistrar
  4. (Optional) Add the view to the Navigation

1. Create the View

Create the View-XAML as needed. In the View-Code, create the WorkspaceFactory. This will tie the View with the correct ViewModel. The ViewModel will need to be registered which will be explained in Step 3.
Demo.xaml.cs
 public class DemoFactory : ChildHomeFactory<Demo>
   {
       public override WorkSpace Generate(RouteValueDictionary parameters, ILifetimeScope scope)
       {
           var vm = scope.Resolve<DemoViewModel>();
           return new WorkSpace()
           {
               Scope = scope,
               View = scope.Resolve<Demo>(
                   new NamedParameter("viewModel", vm)
               ),
               ViewModel = vm
           };
       }
   }

2. Create the ViewModel

In the ViewModel, ensure the class extends BaseViewModel and that the Name is set in the constructor. If the name isn't set in the constructor, the Breadcrumbs will break.
DemoViewModel.cs
  public class DemoViewModel : BaseViewModel
   {
       public PageFrameViewModel PageFrameViewModel { get; set; }
       public class Context : IContext
       {
           public PageFrameViewModel PageFrameViewModel { get; set; }
       }
       readonly Context _ctx;
       public DemoViewModel(Context ctx)
       {
           Name = "Demo";
           PageFrameViewModel = ctx.PageFrameViewModel;
           PageFrameViewModel.Label = "NewDemo";
       }

3. Register the ViewModel in the ViewModelRegistrar

The Registrar is where the WorkspaceFactory looks for the specified ViewModel
ViewModelRegistrar.cs
  builder.RegisterType<DemoViewModel>();

4. (Optional) Add the view to the Navigation Menu

This step is optional. It's only needed if you need the View to be part of the main navigation menu.
HomeViewModel.cs
Add to the constructor
  addNavItem(nameof(Views.Demo),
       "New Nav Page",
       Testing for learning!");