pre Navigation check

Jan 10, 2013 at 1:24 AM

Hi

Is it possible to know before navigating to a page, that the page exists, here is why. In the application we are building the navigation is dynamic and comes from the database, but right now not all pages are built, and I would like to know that the page is not there before calling the page, because if not it is crashing the application. If I trap the error generating by the NavigationManager.NavigateTo, the navigation stack will contain an empty stop that will crash the application later when the back button is clicked.

Hope that is clear enough

thanks

Coordinator
Jan 11, 2013 at 1:17 PM

Hi,

Some form of DoesPageExist(...) method is something I considered in the past. This would probably involve adding this method onto IViewFactory (and maybe onto INavigationManager for completeness). Since this would be a breaking change I avoided it, however there are definately benefits and this is something I'll revisit and maybe implement in the next version.

However, assuming that you are using the default MEF composition there is a workaround. All you really need to know is if a page is defined (pages still work if there is no view-model so this doesn't need to be checked). In your code you can add a new import along the lines of,

[ImportMany("OkraPage")]
private Lazy<object, PageMetadata>[] lazyPages;

Then you can simply enumerate through this list, access the metatdata (provided as part of the lazy-object) and confirm if the desired page exists. Note that since this imports a Lazy<> rather than the page itself then none of the page objects actually get created.

public bool DoesPageExist(string pageName)
{
    foreach(var page in lazyPages)
    {
        if (page.Metadata.PageName == pageName)
            return true;
    }

    return false;
}

Hope this helps,

    Andy

Jan 12, 2013 at 5:36 AM

That's exactly what i was looking for , i should have though of it :-)

 

thanks Andy

Coordinator
Jan 13, 2013 at 10:46 AM

Good to hear that helps. It is a bit of a hack for the moment but should do the job.

Andy

Coordinator
Jan 17, 2013 at 8:49 PM

Hi again,

Just to let you know that I've just pushed some changes to CodePlex source control regarding this feature. There are two new methods, IViewFactory.IsViewDefined(...) and INavigationManager.CanNavigateTo(...) that both do the same check to see if a page exists. Since in most cases you will have imported the navigation manager then the second method is the best to use on most occasions.

Regards,

    Andy

Jan 21, 2013 at 8:30 PM

Hi Andy,

That's great, this is perfect.

Thanks and nice work 

Jan 23, 2013 at 3:04 AM

Hi Andy,

A Quick Note to let you know we appreciate your work and your timely answers to our suggestions and queries. As you can see, I have convinced my Partner "Halloween" to go with Okra (re: my email from Nov 4)! We are "all in" with Okra now. Your recent changes are a great help. When we are rich from our app sales we will remember you!

Keep on the good work.

Frank

 

Coordinator
Jan 23, 2013 at 5:19 PM

Thanks for the message. It's always good to hear that people are finding the framework useful.

Andy