Search code examples
c#wpfmvvmprism

Prism Navigation with Multiple View same ViewModel


I have a Prism module project in my solution called GCode. In this module I have multiple views: GCodeView, MenuView, GCodeEditorView, GCodeProductionView

GCodeView is the main view of this module with a GCodeViewModel, in GCodeModule.cs I'm adding this into a region in another project.

public class GCodeModule : IModule
{
    private readonly IRegionManager _regionManager;
    public GCodeModule(IRegionManager regionManager)
    {
        _regionManager = regionManager;
    }

    public void OnInitialized(IContainerProvider containerProvider)
    {
        _regionManager.RegisterViewWithRegion(RegionNames.GCodeRegion, typeof(GCodeView));

        _regionManager.RequestNavigate(GCodeRegionNames.MenuRegion, nameof(MenuView));
        _regionManager.RequestNavigate(GCodeRegionNames.ContentRegion, nameof(GCodeEditorView));
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<MenuView, MenuViewModel>();
        containerRegistry.RegisterForNavigation<GCodeEditorView, GCodeEditorViewModel>();
        containerRegistry.RegisterForNavigation<GCodeProductionView, GCodeEditorViewModel>();
    }
}

MenuView added to a region on GCodeView. As you see I'm using the same GCodeEditorViewModel for both GCodeEditorView and GCodeProductionView.

I have a ToggleButton on MenuView that swtiches the GCodeEditorView and GCodeProductionView to my `ContentRegion'.

private void ExecuteModeSwitchCommand(bool? isChecked)
        {
            if(isChecked == true)
            {
                _regionManager.RequestNavigate(GCodeRegionNames.ContentRegion, nameof(GCodeEditorView));
            }
            else
            {
                _regionManager.RequestNavigate(GCodeRegionNames.ContentRegion, nameof(GCodeProductionView));
            }
        }

On application start, the GCodeEditorViewModel fires the constructor - that's ok - but at the first time when RequestNavigate to GCodeProductionView, the constructor fires again. After that switching back an forth is not fires the constructor anymore. And that's the goal, but I'm not sure why it's fires it two times.

The whole goal is - maybe I'm approaching from a different way - is to have this module with a menu view and two other view (production/editor) "modes" to see the same "GCode" file.

Note: GCodeProductionView I'm using a ListBox to display the lines on a "GCode" text file. On the GCodeEditorView using a TextBox to display the "GCode" text to be able to edit. I know I can edit in the ListBox, but how can I add a new line if the user press the enter key at anywhere during editing a ListBoxItem?


Solution

  • In GCodeModule.RegisterTypes, register the view model as a singleton before you do the other registrations.. That should make it only be constructed once.

    In other words make this be your first line

        containerRegistry.RegisterSingleton<GCodeEditorViewModel>();