michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

How to keep previous wizard data when we go back #14

Closed tatvasavan closed 3 years ago

tatvasavan commented 3 years ago

Hi Michael,

First, thank you for creating this navigation library.

I am creating a wizard pattern using this library, and I am facing the issue to keep previous wizard data when we navigate back. I think it is a similar issue that was reported earlier. I have checked the suggested way but I think still I am missing something and the Back behavior is not working for me.

Could you please explain, where I need to make the changes to this sample to keep and bind the data to the previous wizard?

michaelscodingspot commented 3 years ago

Hi @savan-bagtharia ,

Can you be a bit more specific? What data exactly are you trying to keep?

tatvasavan commented 3 years ago

Hi Michael,

On the sample application, we can add an employee and for that employee, we can add/set value for the Firstname, LastName, Salary on different wizards, and goto next. Now if we go back to the previous wizard, then all values will not be populated there.

Let's say I have set some values like FirstName = ABC, LastName = XYZ, and Salary = 1000 and after moving to confirm screen, if we go back to other wizards (FirstName, LastName, Salary) then it should display all values.

Based on the documentation, I have changed the value for HistoricalNavigationMode but nothing worked.

Let me know if you need any further details.

michaelscodingspot commented 3 years ago

Ok, I think I understand the problem.

Changing HistoricalNavigationMode to UseSavedParameter won't help because that parameter doesn't do anything in populating the fields in the pages.

I think the best solution for this is a new history mode called "SaveViewModel". It will save a stack of all view models as you advance in history, and going back will use the same ViewModel instance from before. Like with "SaveParamaterInstance" mode, it can lead to memory leaks because you're leaving instances in memory. But I think it's fine for many applications.

Does that sound like a good solution? If so, I can try and implement it.

michaelscodingspot commented 3 years ago

@savan-bagtharia I went ahead and implemented "SaveViewModel" history mode. You can pull latest changes and see how it's used in "Add employee" flow. A new NuGet package (2.3.0) will be available within an hour. I'll update the documentation soon. Enjoy!

tatvasavan commented 3 years ago

@michaelscodingspot Thank you for adding the new mode as SaveViewModel. That is working fine and keeping the data for the previous wizards.

But, I found one behaviour, when we go back to the previous wizard and if we go to the next then data will be lost for the next wizard. e.g. Let's say, I have set values for FirstName = ABC, LastName = XYZ, and Salary = 1000. Now, when we go back to the previous wizard then data will be there (till FirstName). But after that when we go to the next wizard (LastName or Salary) then data is lost.

Can we get some flexibility to choose/decide, whether we want to keep data on Back/Next? I mean it could handle History Navigation completely in both way (start to end/end to start)

michaelscodingspot commented 3 years ago

Sure thing.

As for what you're asking, it's a matter of usage. When clicking "Next" button, the current implementation navigates to a new page and not going forward in history. You can easily change it to check if "Forward" is possible and then navigate forward if yes or navigate to a new page otherwise.

In the sample application, in AddEmployeeViewModel.NextCommand change from

INavigationService navigationService = NavigationServiceProvider.GetNavigationServiceInstance();
AddWizardController controller = navigationService.GetController<AddWizard.AddWizardController>();
controller.Next();
RefreshButtons();

to

INavigationService navigationService = NavigationServiceProvider.GetNavigationServiceInstance();
AddWizardController controller = navigationService.GetController<AddWizard.AddWizardController>();
if (controller.CanGoForward)
{
    controller.GoForward();
}
else
{
    controller.Next();
}
RefreshButtons();
michaelscodingspot commented 3 years ago

@savan-bagtharia Closing this issue as it seems all concern were resolved.