0

I am using Prism 6 with UWP. I have a button in MainPage.xaml which redirect to DashboardPage.xaml. In DashboardPage, I am saving the view state in DashboardPage.xaml.cs :

protected override void SaveState(Dictionary<string, object> pageState)
        {
            base.SaveState(pageState);

            pageState["viewState"] = 123;
        }

and saving view model state in DashboardPageViewModel.cs :

public override void OnNavigatingFrom(NavigatingFromEventArgs e, Dictionary<string, object> viewModelState, bool suspending)
        {
            base.OnNavigatingFrom(e, viewModelState, suspending);
        }

[RestorableState]
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

By pressing back button from topbar, I went back to MainPage.xaml. After that, when I navigate to DashboardPage.xaml again, I found that view state and view model states are being null. Which means, in the below code of DashboardPage.xaml.cs

protected override void LoadState(object navigationParameter, Dictionary<string, object> pageState)
        {
            if (pageState == null)
            {
                return;
            }

            base.LoadState(navigationParameter, pageState);

            if (pageState.ContainsKey("viewState"))
            {
                var data = pageState["viewState"].ToString();
            } 
        }

pageState is found null.

And, for view model state in DashboardPageViewModel.cs :

public async override void OnNavigatedTo(NavigatedToEventArgs e, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(e, viewModelState);
        }

viewModelState is null

Yeasin Abedin
  • 2,081
  • 4
  • 23
  • 41

1 Answers1

2

By pressing back button from topbar, I went back to MainPage.xaml. After that, when I navigate to DashboardPage.xaml again, I found that view state and view model states are being null.

From your posted project. I found that you use NavigationService.Navigate to navigate to DashboardPage. It's right at first time, but after you navigating back to MainPage and again navigate back to DashboardPage. You are also using the NavigationService.Navigate API.

I downloaded Prism's Source Codes and found where the LoadState is called:

protected override void OnNavigatedTo(NavigationEventArgs navigationEventArgs)
{
        ...
    if (navigationEventArgs.NavigationMode == NavigationMode.New)
    {
        var nextPageKey = _pageKey;
        int nextPageIndex = frameFacade.BackStackDepth;
        while (frameState.Remove(nextPageKey))
        {
            nextPageIndex++;
            nextPageKey = "Page-" + nextPageIndex;
        }

        // Pass the navigation parameter to the new page
        LoadState(navigationEventArgs.Parameter, null);
    }
    else
    {
        LoadState(navigationEventArgs.Parameter, (Dictionary<String, Object>)frameState[_pageKey]);
    }
}

As you can see, the LoadState is called inside OnNavigatedTo and PageState is passed as argument on condition that the NavigationMode is not New. For ViewModelState it is the similar situation. And for your case, everytime navigating to a page through NavigationService.Navigate will create a totally new page, which means NavigationMode=NavigationMode.New. Therefore PageState and ViewModelState are null.

From the NavigationMode document we can see the NavigationMode.Forward fits your requirement.

So, to fix the problem. The only thing that needs to be done is to modify the OnPageChange method in MainPageViewModel.cs like codes below:

private void OnPageChange()
{
    if (_navigationService.CanGoForward())
    {
        _navigationService.GoForward();
    }
    else
    {
        _navigationService.Navigate("Dashboard", null);
    }
}
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24