Reload page in metro app C#

13,060

Solution 1

This will refresh your page:

var _Frame = Window.Current.Content as Frame;
_Frame.Navigate(_Frame.Content.GetType());
_Frame.GoBack(); // remove from BackStack
  • Handling OnNavigatingFrom() you can save your page's data and state.
  • Handling OnNavigatingTo() you can load your page's data and state.

As a caveat, my sample does not account for page parameters, you may need to. Also, another caveat, my sample reloads your page twice. But the GoBack() is necessary to remove the new entry from the BackStack. Unlike WP, Frame does not have Refresh(). Also, the BackStack does not have Remove().

UPDATE

I no longer use the above approach. I use this:

public bool Reload() { return Reload(null); }
private bool Reload(object param)
{
    Type type = this.Frame.CurrentSourcePageType;
    if (this.Frame.BackStack.Any())
    {
        type = this.Frame.BackStack.Last().SourcePageType;
        param = this.Frame.BackStack.Last().Parameter;
    }
    try { return this.Frame.Navigate(type, param); }
    finally { this.Frame.BackStack.Remove(this.Frame.BackStack.Last()); }
}

Solution 2

I’m not sure I fully understand what you are trying to do, so this may be wrong.

By calling that line of code when you are refreshing the page, you are creating a brand new object of the current type and navigating to it, so this does not save changes the user makes while they are on the current page.

Are you using any type of design pattern? For things like this I use MVVM (using the MVVM light library) which implements a really cool navigation service which will keep stuff like this in check.

Share:
13,060
Raj Kumar
Author by

Raj Kumar

Hi there, My name is Raj Kumar and I've been working on Android over 4 years now.I have a good experience designing mobile apps governed by HIG(Human Interface Guidelines). We personally designed development framework to minimize development time where most of the functionalities are pre-developed (like WebServices API integration, Local database listing, Social Network integration).

Updated on June 16, 2022

Comments

  • Raj Kumar
    Raj Kumar almost 2 years

    I'm developing metro app using Windows 8 RTM and C#(VS 2012 RTM), I'm stuck with page reload, Can any one explains me how to reload page with out navigating to same page again. Brief: I'm developing metro app with multilingual support. When user selects the language I'm overriding primary language by below code

    Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "de";
    

    and reload the page by using this code

    this.Frame.Navigate(this.GetType());
    

    Language changed to "de",But when i press "Back" on page its navigating same page instead of navigating to previous page.Did i miss something, Can someone please explains me how to do this. Thanks in advance

  • Raj Kumar
    Raj Kumar over 11 years
    Thanks lookitskris, But I'm not using MVVM pattern here, is there any way that i can reload page with out creating new object,Please help me.
  • lookitskris
    lookitskris over 11 years
    check out the Refresh method on the Frame object
  • Joris Weimar
    Joris Weimar over 11 years
    for anyone who cares: when using (complex) page parameters, just store the parameter in your page base class. then you just pass it in: _Frame.Navigate(..., savedParameter). To prevent loading twice (something to worry about in case you have heavy loading in NavigatedTo) is setting a global flag "IsReloading". In onnavigatedto if set to true, cancel further loading and set it to false.
  • Jerry Nixon
    Jerry Nixon over 11 years
    Samples are so much better than paragraphs.
  • Joris Weimar
    Joris Weimar over 11 years
    contributions are so much better than critiques
  • Geotinc
    Geotinc over 10 years
    It works only if Frame is not saved with SuspensionManager. If the frame has been save with SuspensionManager, the GoBack method displays the page with the same language as previously. Any idea ?
  • Jerry Nixon
    Jerry Nixon over 10 years
    Yes, navigate to the correct page and pass the correct parameter.
  • vent
    vent over 9 years
    Also, if your page has NavigationCacheMode, set it to Disabled first this.NavigationCacheMode = NavigationCacheMode.Disabled;
  • Stephen Hosking
    Stephen Hosking about 8 years
    Looks like Refresh is not available on a Frame in WinRT. msdn.microsoft.com/en-us/library/…