Ali Demirci
Ali Demirci

Reputation: 5442

How to cast SourcePageType as Page?

Well I'm building a WinRT app currently and I am facing this issue.

What I am trying to achieve is that I need to read or modify the properties on previous page that I am navigated from.

So let's say that I have two pages: MainPage and WatchPage.

I am navigating to WatchPage from MainPage and inside the WatchPage's OnNavigatedTo event I need to access a property inside MainPage without using navigation parameters...

How can I achieve this?

protected override void OnNavigatedTo(NavigationEventArgs e){
    Type LastPage = rootFrame.BackStack.Last().SourcePageType;

    if(LastPage == typeof(MainPage){
        // here, for example, say that MainPage has a property called isLoadSuccess
        // I need to set that property to false
        // or call a method from MainPage
        // => this is not working MainPage MP = LastPage as MainPage;
        // I know that it should not work anyway, but, how can I achieve this?
    }
}

Upvotes: 0

Views: 360

Answers (2)

Jerry Nixon
Jerry Nixon

Reputation: 31831

What you are attempting to do cannot be accomplished using the existing framework. That is to say, a reference to the instance of your previous page does not exist in the BackStack. The only thing that exists is, honestly the recipe to re-create the instance. That is to say, the BackStack contains the type and the parameter. With those two elements, you should be able to re-instantiate the class when the user navigates back to it in the Frame. These ingredients, by the way, include the type and the serializable parameter - only with a serializable parameter can the app save the BackStack to NavigationState and reliably restore it should your application be suspended/terminated.

That's the story, now to your problem. I see several possible solutions, to be honest, and I think any of them are acceptable.

The first caveat is that you MUST use NavigationCacheMode = Enabled. Without this, your page (including the previous page) will be re-created with every navigation, including Back. This is an important behavior of UWP because it assumes your page is removed from memory to save the overall footprint. Setting NavigationCacheMode will override that.

  1. Create a static reference

This is easy enough right? Something like this:

public readonly static MainPage Instance;
public MainPage() {
    Instance = this;
} 

Beware of this, because it requires your views to have a reference to each other. This is a pretty minimal risk, IMHO.

  1. Pass a reference

This is easiest to accomplish with Navigate(parameter) like this.

// MainPage
public void GoAway() {
    this.Frame.Navigate(typeof(SecondPage), this);
} 

// SecondPage
MainPage MainPage;
public void OnNavigatedTo(object parameter) {
    MainPage = parameter as MainPage;
} 

Beware of this because it requires you to pass something that cannot be serialized. Sometimes, though, this is acceptable.

Any of those make sense?

Best of luck.

Upvotes: 1

Nasser AlNasser
Nasser AlNasser

Reputation: 1775

I'd recommend to create a static class with static properties and methods.. I don't think what you try to achieve is a good practice.. instead, here's my example:

   public static class Shared
    {
        public static string property = "something..";

        public static async Task<bool> Method()
        {
            await Task.Delay(400);
            return false;
        }
    }

Then, you can call the method, get or set the property from any where:

bool result = await Shared.Method();

Upvotes: 0

Related Questions