开发者

How to load MainPage when goBack(); form SecondPage?

I use NavigationService.Navigate("..."). But I want to load MainPage again when I click the Back button by event 开发者_运维问答goBack();


In your goBack method you can use the NavigationService.GoBack() call to move from SecondPage back to the MainPage. You can view all the Navigation methods available to you on MSDN.


You can override the method OnBackKeyPress, and inside the method write code shown below, it ll navigate to MainPage.xaml

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}


If you are using binding do this:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       App.ViewModel.LoadData();

    }

And in MainViewModel.cs

public void LoadData() { Items.Clear();
this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Data info 2" }); this.IsDataLoaded = true; }


Like Nigel said, you can use NavigationService.GoBack() however this will only work if you went from the MainPage to the second page.

The only other way to do this is how you have mentioned using NavigationService.Navigate(uri).


Override the navigated to event

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
       if (PhoneApplicationService.Current.State["msg"].ToString() == "from page2")
       {
         // do something when its comming from page2
       }
        base.OnNavigatedTo(e);
    }

Now that will trigger every time its navigated to but you could send some parameters so you know what to depending on what page it came from.

PhoneApplicationService.Current.State["msg"] = "from page2";
NavigationService.Navigate(new Uri("/MainPage.xaml?msg=", UriKind.Relative));


There are two ways to get back to your MainPage.

(First Method) Override the OnBackKeyPress() and write the following snippet inside it.

NavigationService.GoBack();

Note :

This will move to the MainPage only if the MainPage is the previous page in your Navigation history.

(Second Method) Override the OnBackKeyPress() and write the following snippet inside it.

NavigationService.Navigation(new Url("/MainPage.xaml",UriKind.Relative));

Note :

This will create a New MainPage and navigates to it. So if you navigated from MainPage to SecondPage and if you navigate again to MainPage using the above snippet, you will be having two MainPage in your Navigation history.

So, use any one of the snippet according to your scenario.


if WP7 or WP8 : NavigationService.GoBack()

if WP8.1 : Frame.GoBack(); and you can Override the navigated to event

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {

// do nothing  }


I don't recommend overriding the OnBackKeyPress method and calling NavigationService.Navigate method within it as it may mess up your page back stack.

I assume your app has more than 2 pages. Otherwise you should just call NavigateService.GoBack() as others have recommended.

Example

  1. Start on MainPage (backstack: empty)
  2. Navigate to SecondPage (backstack: MainPage)
  3. Navigate to ThirdPage (backstack: SecondPage, MainPage)
  4. Click the back button (overridden) and you are taken to MainPage (backstack: ThirdPage, SecondPage, MainPage)
  5. Click the back button and you are taken to the ThirdPage (backstack: SecondPage, MainPage). This is not the expected behaviour as the application should have exited.
  6. Now you're stuck in a back button loop.

Solution

Override OnNavigatingFrom and remove any of the backstack entries you don't want the user to go to. You can either allow the user to press the back button to get back to the MainPage from the ThirdPage or you can call NavigationService.GoBack().

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    // Remove all backstack entries except for the first one
    while(NavigationService.BackStack.Count() > 1)
    {
        NavigationService.RemoveBackEntry();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜