Prism icon indicating copy to clipboard operation
Prism copied to clipboard

[XF] How to navigate to a NavigationPage

Open odedlevy opened this issue 11 years ago • 43 comments

When I am required to navigate from within a certain page and want to use Xamarin NavigationPage option to display toolbar items, the initiator of the navigation needs to wrap his content page inside a navigation page. When trying to navigate using prism (e.g. NavigationService.Navigate(“my_view_name”);) I have no control of the page initiation and cannot wrap the page with a NavigationPage. I think that Prism should enable to provide a PageProvider when registering the page for navigation using RegisterTypeForNavigation method

odedlevy avatar Jun 23 '15 16:06 odedlevy

This is actually a good idea. How do you envision the API working to enable this feature? Using an attribute on the View, maybe something like a NavigationPageAttribute? How about an overload of the NavigationService.Navigate method, something like Navigate("View", useNavigationPage: true)? Any other ideas?

brianlagunas avatar Jun 23 '15 17:06 brianlagunas

Hi Brian,

At first I though the same as what you recommended (Navigate("View", useNavigationPage: true)) the problem is that there are currently 5 different page options and in the future they may be more. [image: Inline image 1]

Therefore this lead to the option of defining an IPageNavigationProvider let looks something like: public interface IPageNavigationProvider { Page BeforeNavigatePeek(Page pageToBeNavigatedTo); }

and when defining the RegisterTypeForNavgation there will be an overload option that will except the IPageNavigationProvider. Before navigation prism will call the BeforeNavigatePeek enabling the user to return the page wrapped in a NavigationPage or any other option.

The weak spot of this option is that it will require Prism to reference Xamarin (assuming it is not already familiar with it) so that it will be able to expose the "Page" object. If required, the Page can be replaced with an object. Not ideal but an option: public interface IPageNavigationProvider { object BeforeNavigatePeek(object pageToBeNavigatedTo); }

I must state that the Xamarin design does not feel right. The one that should decide on the it's layout is the opened page and not the opening page. In addition it is the opened page that defines the toolbar items causing the NavigationPage logic to be split between 2 pages instead of 1. Not the best design.

I'll be glad to hear your opinion.

Regards Oded.

On Tue, Jun 23, 2015 at 8:36 PM, Brian Lagunas [email protected] wrote:

This is actually a good idea. How do you envision the API working to enable this feature? Using an attribute on the View, maybe something like a NavigationPageAttribute? How about an overload of the NavigationService.Navigate method, something like Navigate("View", useNavigationPage: true)? Any other ideas?

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-114583092.

*Oded Levy, *Co-Founder and Manager Razor Grip Solutions Mobile : 054-5442069 Email : [email protected] Web: www.razorgrip.com

odedlevy avatar Jun 24 '15 08:06 odedlevy

I'm curious if it would be easier to just create a class the derives from NavigationPage and just register that with the navigation framework.

public class MyNavigationPageA : NavigationPage {....}

Container.RegisterTypeForNavigation<MyNavigationPageA>();

Or something like that. I haven't really thought this through, but it's just an idea that popped in my head. Whatever the solution, it should not involve having the ViewModel know about a View type.

brianlagunas avatar Jun 24 '15 16:06 brianlagunas

I already tried that unfortunately that does not work in Xamarin. You cannot have a page of type NavigationPage. It compiles but crashes at runtime. I also tries creating a NavigationPage (in XAML) that contains a ContentPage but still no luck.

In any case it is not the viewmodel that knows about the type of view, rather the IPageNavigationProvider which is part of the bootstrapper. It is something that the application level decides on which feels reasonable to me.

Note that currently using Prism eliminates the option of having anything besides a ContentPage which is very limiting.

Can you think of a simple solution to overcome the problem within the current version of Prism that does not require replacing the entire logic of Prism's NavigationService?

Thanks

On Wed, Jun 24, 2015 at 7:38 PM, Brian Lagunas [email protected] wrote:

I'm curious if it would be easier to just create a class the derives from NavigationPage and just register that with the navigation framework.

public class MyNavigationPageA : NavigationPage {....}

Container.RegisterTypeForNavigation();

Or something like that. I haven't really thought this through, but it's just an idea that popped in my head. Whatever the solution, it should not involve having the ViewModel know about a View type.

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-114936417.

*Oded Levy, *Co-Founder and Manager Razor Grip Solutions Mobile : 054-5442069 Email : [email protected] Web: www.razorgrip.com

odedlevy avatar Jun 24 '15 18:06 odedlevy

I'll have to think about this one. Hopefully the community can come together to help solve this one.

brianlagunas avatar Jun 25 '15 03:06 brianlagunas

Ok. Great talking with you. Please update if you reach any breakthrough.

Oded. On Jun 25, 2015 6:02 AM, "Brian Lagunas" [email protected] wrote:

I'll have to think about this one. Hopefully the community can come together to help solve this one.

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-115085670.

odedlevy avatar Jun 25 '15 05:06 odedlevy

I was just going to ask about this. Developing not only for mobile but full desktop systems will eventually require RegionManager style navigation. But being able to design a shell with NavigationPage is an absolute minimum.

Look forward to any progress with this.

thomashagstrom avatar Jun 26 '15 12:06 thomashagstrom

One way could be to define the navigation options as enum and then add a factory interface as well as page presenter interface.

Rought sketch

public enum class NavigationOptions { Push = 0, Modal = 1, ModalNavigationBar = 2, ... }

public interface IPageNavigationPresenterFactory {
    IPageNavigationPresenter Presenter(NavigationOptions navigationOption);
}

public interface IPageNavigationPresenter {
     Page Present(Page page);
}

public class CustomModalNavigationPagePresenter : IPageNavigationPresenter {
    Page Present(Page page) {
      return new MyCustomNavigationPage(page);
    }

For pages used interchangely as modal/pushed depending on the application state new issues will arise when trying to close the page. For example at application startup the MainViewModel launches and in ViewDidAppear we check if settings are valid.

If not the SettingsViewModel is navigated to modally and then login is pressed. When logged in the ClosePage() is called and in here the PopModal need to be called. But if navigated to from e.g a MasterDetailPage scenario then the ClosePage() should just call Pop.

Don't know if it is clear what I mean. If i have time I can implement a sample project to illustrate the issue

joacar avatar Aug 28 '15 07:08 joacar

My first stab at this will use an attribute on the view. Something like [NavigationOptions(PageType.NavigationPage)]. Expect a PR for review soon.

brianlagunas avatar Oct 09 '15 00:10 brianlagunas

Okay, so after implementing this, I think it may be better to Provide an attribute that accepts an IPageProvider. So something like [NavigationOptions(PageProviderType=typeof(ViewAPageProvider)].

Then it could be passed the view being navigated to so that properties can be set on it, then it could also return a different page to act as a wrapper/target page like a NavigationPage.

What do you think about that?

Edit: Actually the more I play with it, the more I want to ditch the NavigationOptionsAttribute in favor of just a NavigationPageProviderAttribute. Then provide a Type property for the INavigationPageProvider and use service locator to resolve it so that any dependencies the provider may need can be constructed.

brianlagunas avatar Oct 09 '15 02:10 brianlagunas

Hi Brian,

I see that you were quick in solving the issue which is great news.

From a design point of view I prefer to avoid attributes, even though your solution that includes a provider enables the user to place logic when required. The problem is that the provider will not be created by the UnityContainer and therefore if the container is required, will need to receive it using the ServiceLocator which is sometimes not a good solution (in cases where there is more then one container in use). In addition the provider may need access to the current state such as who navigated to the window and such. Replacing the IProvider for testing will also be problematic. On the other hand , since I am not familiar with how Prism implemented the Navigation, though I assume it relies on reflection, therefore the use of the attribute one of the ways to pass out the logic required for the navigation.

Regards Oded.

On Fri, Oct 9, 2015 at 5:18 AM, Brian Lagunas [email protected] wrote:

Okay, so after implementing this, I think it may be better to Provide an attribute that accepts an IPageProvider. So something like [NavigationOptions(PageProviderType=typeof(ViewAPageProvider)].

Then it could be passed the view being navigated to so that properties can be set on it, then it could also return a different page to act as a wrapper/target page like a NavigationPage.

What do you think about that?

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-146738059.

*Oded Levy, *Co-Founder and Manager Razor Grip Solutions Mobile : 054-5442069 Email : [email protected] Web: www.razorgrip.com

odedlevy avatar Oct 10 '15 19:10 odedlevy

Actually @odedlevy, the container is used to create the provider. So you would have no problem injecting dependencies into your provider class. Prism navigation doesn't use reflection, but the addition of this attribute does require reflection to check for the attribute. For this reason, we cache the result so we are not incurring the cost everytime.

Is it common to have a reference to the source page when navigating to another taregt page in order to configure properties on the target? Testing the INavigationPageProvider should not be an issue at all.

Since you are not a fan of the attribute, how would you like to see the API?

brianlagunas avatar Oct 10 '15 21:10 brianlagunas

There are 2 options that I can think of:

  1. If we assume that there is only a single page provider for the entire app - I would consider to register it on the INavigationService during the app bootstrap. (e.g. add a function to INavigfationService such as RegisterPageProvider(IPageProvider) or by implementing a default IPageProvider and only when user registers a different one on the container use his).
  2. A different option in case you prefer multiple providers is to add an overload to the INavigationService.Navigate that can accept the IPageProvider.

Note that I am not familiar with the solution you implemented and if actually relies heavily on reflection , yet I assume that these options are possible.

What do you think?

Oded

On Sun, Oct 11, 2015 at 12:14 AM, Brian Lagunas [email protected] wrote:

Actually @odedlevy https://github.com/odedlevy, the container is used to create the provider. So you would have no problem injecting dependencies into your provider class. Prism navigation doesn't use reflection, but the addition of this attribute does require reflection to check for the attribute. For this reason, we cache the result so we are not incurring the cost everytime.

Is it common to have a reference to the source page when navigating to another taregt page in order to configure properties on the target? Testing the INavigationPageProvider should not be an issue at all.

Since you are not a fan of the attribute, how would you like to see the API?

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-147126535.

*Oded Levy, *Co-Founder and Manager Razor Grip Solutions Mobile : 054-5442069 Email : [email protected] Web: www.razorgrip.com

odedlevy avatar Oct 10 '15 21:10 odedlevy

We definitely need to consider the need for multiple providers. I also thought of adding an overload to the INavigationService interface to accept an INavigationPageProvider. This would remove the need for reflection to check the attribute. Another benefit to placing it on the navigation service is that you can provide different providers even when navigating to the same type of view. This is a tough one.

brianlagunas avatar Oct 10 '15 22:10 brianlagunas

So where we at, is there a branch where this can be tested yet?

I don't see any mention of a RegionManager, but as long as we can put content around a navigation page and have Prism target all navigation to that NavigationPage control (sorry, "view"..) then I'm good :)

thomashagstrom avatar Oct 11 '15 15:10 thomashagstrom

No regions, but there is an approach to wrapping the target page in a navigationPage or other type of page. See PR #195 for details. This PR has already been merged. Now, I am trying to decide if this should remain an attribute, or become something you must provide in the NavgationService.Navigate signature.

brianlagunas avatar Oct 11 '15 15:10 brianlagunas

If it's only possible to use one NavigationPage per app, in reality, then the KISS principle should apply :)

thomashagstrom avatar Oct 11 '15 17:10 thomashagstrom

Well, you can actually return any type of Page from this interface, such as Master/Detail. Also, you can have multiple XF.NavigationPages per app. So I think I am going to remove the attribute and rely on the parameter on the Navigate method.

brianlagunas avatar Oct 11 '15 17:10 brianlagunas

Now I have an issue. If I do that, now the ViewModel will know about INavigationPageProvider (which relies on the XF.Page type in its members). Is this a big deal? I wanted to keep all references of XF.Page out of the ViewModel and any class that the ViewModel needs.

This also opens the VM up to inappropriate touching since using the INavigationProvider in the VM will give the access to both the source page and target page before navigation has completed.

Thoughts?

brianlagunas avatar Oct 11 '15 18:10 brianlagunas

My first thought is to replace the 4-5 page options with an enum. I dont see too many options for additional page types in the future. An alternative is to return a .net "object" instead of a page and you can try to safely downcast it. On Oct 11, 2015 9:04 PM, "Brian Lagunas" [email protected] wrote:

Now I have an issue. If I do that, now the ViewModel will know about INavigationPageProvider (which relies on the XF.Page type in its members). Is this a big deal? I wanted to keep all references of XF.Page out of the ViewModel and any class that the ViewModel needs.

Thoughts?

— Reply to this email directly or view it on GitHub https://github.com/PrismLibrary/Prism/issues/64#issuecomment-147230882.

odedlevy avatar Oct 11 '15 18:10 odedlevy