Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Automatic View Registration #1757

Closed
dansiegel opened this issue Apr 5, 2019 · 3 comments
Closed

[Proposal] Automatic View Registration #1757

dansiegel opened this issue Apr 5, 2019 · 3 comments
Labels

Comments

@dansiegel
Copy link
Member

Description

Since it can be assumed that any page within a Xamarin Forms Project will be used for Navigation we can dramatically simplify View Registration by introducing an Opt-In API that would allow Prism to reflect on the main assembly (where App.xaml.cs is located), and any modules that are loaded)

Proposal

Core support for this feature would come from the use of two new Attributes RegisterByConventionAttribute and RegisterForNavigationAttribute.

public class RegisterByConventionAttribute : Attribute
{
    public bool Automatic { get; set; }
}

public class RegisterForNavigationAttribute : Attribute
{
    public Type ViewType { get; }
    public Type ViewModelType { get; }
    public string Name { get; }
}

To Opt-In you will need to decorate your App class with the RegisterByConvention attribute like so:

[RegisterByConvention]
public partial class App : PrismApplication
{
}

When set like this Prism would then reflect over the assembly:

  1. First it would look for attributes set at an assembly level like:
[assembly: RegisterForNavigation(typeof(ViewA))]
[assembly: RegisterForNavigation(typeof(ViewB), typeof(ViewBViewModel))]
[assembly: RegisterForNavigation(typeof(ViewC), "viewC")]
[assembly: RegisterForNavigation(typeof(ViewD), typeof(ViewDViewModel), "viewD")]

In this example we would get 4 automatically registered Views

  1. In the event that we have opted for this sort of automatic registration, but no registrations are found at an assembly level, PrismApplication would then reflect over the exported types and evaluate any types that inherit from Page. This would occur for the assembly that the implementing App class is located in as well as for any loaded Modules. For this the Page type must declare the RegisterForNavigation attribute
[RegisterForNavigation]
public class ViewA : ContentPage
{
}

[RegisterForNavigation("viewB")
public class ViewB : ContentPage
{
}

[RegisterForNavigation { ViewModelType = typeof(ViewCViewModel) }]
public class ViewC : ContentPage
{
}
  1. As shown above the RegisterByConventionAttribute has an Automatic property. In the event that Automatic is set, any assembly level attributes will be handled, and all Exported types will be evaluated. In the event that a given Page type is not handled by either an assembly or type level RegisterForNavigationAttribute, the type will be automatically registered by convention with the type name as the navigation key.
@dansiegel dansiegel added the XF label Apr 5, 2019
@brianlagunas
Copy link
Member

implementd with PR #1780

@Im-PJ
Copy link

Im-PJ commented Jul 25, 2019

[AutoRegisterForNavigation(Automatic = true)]
public class App : PrismApplication
{
}

It can't recognize Automatic property. I have update to prism 7.2

@lock
Copy link

lock bot commented Jan 28, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants