Вопрос

I have the case in Prism where I support the SearchContract with View/ViewModel combination.

The problem is this: when I leave the Search Panel open and perform multiple searches, each individual search is placed on the Navigation Stack. This means that when the user hits the "GoBack" button, they see each previous search result until they reach the original form that was being displayed before search was initiated.

I want the GoBack button to navigate back to the first page prior to Search.

I see a couple of possibilities:

  1. Change GoBack to unwind the stack until the first non-search view is reached.
  2. Do something in NavigateFrom to pop the current view from the stack before Navigating away form the active search.
  3. Do the same thing in #2 only on NavigateTo
  4. Use Regions?
Это было полезно?

Решение

One approach is to navigate.GoBack() on the SearchPage.OnNavigatedTo event when the navigation mode is 'Back.

public override async void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, System.Collections.Generic.Dictionary<string, object> viewModelState)
{

        if (navigationMode != NavigationMode.Back)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            // ... and so on ...
        }
        else
        {
            if (this.navigationService.CanGoBack())
            {
                // this call must be run on the dispatcher due to using the async-void signature on this event
                await Window.Current.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.navigationService.GoBack());
            }
        }
    }

What's interesting here is the need to use the Dispatcher. Without it, the .GoBack() function fails. I'm not convinced this is the best answer, but it's an answer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top