Difference between revisions of "Navigation and Routing"

From truxwiki.com
Jump to navigation Jump to search
Line 41: Line 41:
 
<div style="display:flex;justify-content:center;">   
 
<div style="display:flex;justify-content:center;">   
 
{| class="wikitable"
 
{| class="wikitable"
! Example
+
! Demo.xaml.cs
 
|-
 
|-
| Views/Home/Demo.xaml
+
|  
 
   public class DemoFactory : ChildHomeFactory<Demo>
 
   public class DemoFactory : ChildHomeFactory<Demo>
 
     {
 
     {
Line 67: Line 67:
 
<div style="display:flex;justify-content:center;">   
 
<div style="display:flex;justify-content:center;">   
 
{| class="wikitable"
 
{| class="wikitable"
! Example
+
! DemoViewModel.cs
 
|-
 
|-
| \TruxtonClient\ViewModels\Media\DemoViewModel.cs
+
|  
 
   public class DemoViewModel : <b>BaseViewModel</b>
 
   public class DemoViewModel : <b>BaseViewModel</b>
 
     {
 
     {
Line 92: Line 92:
 
<div style="display:flex;justify-content:center;">   
 
<div style="display:flex;justify-content:center;">   
 
{| class="wikitable"
 
{| class="wikitable"
! Example
+
! ViewModelRegistrar.cs
 
|-
 
|-
| \TruxtonClient\PresentationCore\ViewModelRegistrar.cs
+
|  
 
   builder.RegisterType<DemoViewModel>();
 
   builder.RegisterType<DemoViewModel>();
 
|-
 
|-

Revision as of 12:03, 2 November 2021

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

Main Files

File 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


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!");