Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload page in metro app C#

Tags:

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

like image 917
Raj Kumar Avatar asked Sep 10 '12 09:09

Raj Kumar


2 Answers

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()); }
}
like image 200
Jerry Nixon Avatar answered Oct 14 '22 17:10

Jerry Nixon


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.

like image 43
lookitskris Avatar answered Oct 14 '22 16:10

lookitskris