forked from PrismLibrary/Prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for View Registration by Attribute or automatic View Reg…
…istration.
- Loading branch information
Showing
6 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
namespace Prism.AppModel | ||
{ | ||
/// <summary> | ||
/// Represents the Platform (OS) that the application is running on. | ||
/// </summary> | ||
/// <remarks>This enum acts as a wrapper around the Device.RuntimePlatform string-based options</remarks> | ||
public enum RuntimePlatform | ||
{ | ||
Android, | ||
iOS, | ||
macOS, | ||
Tizen, | ||
UWP, | ||
WinPhone, | ||
WinRT, | ||
Unknown | ||
} | ||
/// <summary> | ||
/// Represents the Platform (OS) that the application is running on. | ||
/// </summary> | ||
/// <remarks>This enum acts as a wrapper around the Device.RuntimePlatform string-based options</remarks> | ||
public enum RuntimePlatform | ||
{ | ||
Android, | ||
iOS, | ||
macOS, | ||
Tizen, | ||
UWP, | ||
WinPhone, | ||
WinRT, | ||
Unknown | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Source/Xamarin/Prism.Forms/Ioc/RegisterByConventionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace Prism.Ioc | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | ||
public class RegisterByConventionAttribute : Attribute | ||
{ | ||
public bool Automatic { get; set; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Source/Xamarin/Prism.Forms/Ioc/RegisterForNavigationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Prism.AppModel; | ||
using System; | ||
|
||
namespace Prism.Ioc | ||
{ | ||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = true)] | ||
public class RegisterForNavigationAttribute : Attribute | ||
{ | ||
public Type ViewType { get; set; } | ||
public Type ViewModelType { get; set; } | ||
public string Name { get; set; } | ||
public RuntimePlatform? RuntimePlatform { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Prism.Mvvm; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Xamarin.Forms; | ||
|
||
namespace Prism.Ioc | ||
{ | ||
internal static class IContainerRegistryAutoLoadExtensions | ||
{ | ||
public static void AutoRegisterViews(this Type type, IContainerRegistry containerRegistry) | ||
{ | ||
if (!type.GetCustomAttributes().Any(a => a is RegisterByConventionAttribute)) return; | ||
|
||
var regAttr = type.GetCustomAttribute<RegisterByConventionAttribute>(); | ||
var assembly = type.Assembly; | ||
|
||
if (regAttr.Automatic) | ||
{ | ||
var viewTypes = assembly.ExportedTypes.Where(t => t.IsSubclassOf(typeof(Page))); | ||
RegisterViewsAutomatically(containerRegistry, viewTypes); | ||
} | ||
else | ||
{ | ||
RegisterViewsByAttribute(containerRegistry, assembly); | ||
} | ||
} | ||
|
||
private static void RegisterViewsAutomatically(IContainerRegistry containerRegistry, IEnumerable<Type> viewTypes) | ||
{ | ||
foreach (var viewType in viewTypes) | ||
{ | ||
containerRegistry.RegisterForNavigation(viewType, viewType.Name); | ||
} | ||
|
||
if (!containerRegistry.IsRegistered<object>(nameof(NavigationPage))) | ||
{ | ||
containerRegistry.RegisterForNavigation<NavigationPage>(); | ||
} | ||
|
||
if (!containerRegistry.IsRegistered<object>(nameof(TabbedPage))) | ||
{ | ||
containerRegistry.RegisterForNavigation<TabbedPage>(); | ||
} | ||
} | ||
|
||
private static void RegisterViewsByAttribute(IContainerRegistry containerRegistry, Assembly assembly) | ||
{ | ||
var attrs = assembly.GetCustomAttributes<RegisterForNavigationAttribute>(); | ||
foreach (var attr in attrs) | ||
{ | ||
RegisterViewByAttribute(containerRegistry, attr); | ||
} | ||
|
||
var viewTypes = assembly.ExportedTypes.Where(t => t.IsSubclassOf(typeof(Page)) | ||
&& t.GetCustomAttributes<RegisterForNavigationAttribute>().Any()); | ||
foreach (var viewType in viewTypes) | ||
{ | ||
var attr = viewType.GetCustomAttributes<RegisterForNavigationAttribute>() | ||
.FirstOrDefault(a => a.RuntimePlatform.ToString() == Device.RuntimePlatform || a.RuntimePlatform is null); | ||
attr.ViewType = viewType; | ||
RegisterViewByAttribute(containerRegistry, attr); | ||
} | ||
} | ||
|
||
private static void RegisterViewByAttribute(IContainerRegistry containerRegistry, RegisterForNavigationAttribute attr) | ||
{ | ||
if (attr.ViewType is null) | ||
throw new Exception($"Cannot auto register View. No ViewType was specified. Name: '{attr.Name}'. ViewModelType: '{attr.ViewModelType?.Name}'"); | ||
|
||
if (attr.RuntimePlatform != null && attr.RuntimePlatform.ToString() != Device.RuntimePlatform) return; | ||
|
||
var name = attr.Name; | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
name = attr.ViewType.Name; | ||
} | ||
|
||
containerRegistry.RegisterForNavigation(attr.ViewType, name); | ||
|
||
if (attr.ViewModelType != null) | ||
{ | ||
ViewModelLocationProvider.Register(attr.ViewType.Name, attr.ViewModelType); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters