diff options
author | Brian Lagunas <brian.lagunas@live.com> | 2017-05-16 09:11:32 +0300 |
---|---|---|
committer | Brian Lagunas <brian.lagunas@live.com> | 2017-05-16 09:11:32 +0300 |
commit | 8d82ba3313772c03faf3f92767ae1b7575acdfab (patch) | |
tree | a898db2598d3f2bb46d58ba34e01b97fc43df263 | |
parent | af468871b3f017462d86db657d68dff468f3e666 (diff) |
added new IPlatform for nav registration
-rw-r--r-- | Source/Xamarin/Prism.Autofac.Forms/AutofacExtensions.cs | 68 | ||||
-rw-r--r-- | Source/Xamarin/Prism.DryIoc.Forms/DryIocExtensions.cs | 66 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Forms/AppModel/RuntimePlatform.cs (renamed from Source/Xamarin/Prism.Forms/Services/RuntimePlatform.cs) | 4 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Forms/Platform{T}.cs | 24 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Forms/Prism.Forms.csproj | 3 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Forms/Services/DeviceService.cs | 3 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Forms/Services/IDeviceService.cs | 3 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Ninject.Forms/NinjectExtensions.cs | 66 | ||||
-rw-r--r-- | Source/Xamarin/Prism.Unity.Forms/UnityExtensions.cs | 289 |
9 files changed, 360 insertions, 166 deletions
diff --git a/Source/Xamarin/Prism.Autofac.Forms/AutofacExtensions.cs b/Source/Xamarin/Prism.Autofac.Forms/AutofacExtensions.cs index a766e94..84be52f 100644 --- a/Source/Xamarin/Prism.Autofac.Forms/AutofacExtensions.cs +++ b/Source/Xamarin/Prism.Autofac.Forms/AutofacExtensions.cs @@ -66,7 +66,7 @@ namespace Prism.Autofac.Forms /// <param name="windowsView">Windows Specific View Type</param> /// <param name="winPhoneView">Windows Phone Specific View Type</param> /// <returns><see cref="IUnityContainer"/></returns> - [Obsolete("RegisterTypeForNavigationOnPlatform is obsolete. Use switch(Device.RuntimePlatform) instead.")] + [Obsolete("This signature of the RegisterTypeForNavigationOnPlatform method is obsolete due to Device.OnPlatform being deprecated. Use the new IPlatform[] overload instead.")] public static IContainer RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, string name = null, Type androidView = null, Type iOSView = null, Type otherView = null, Type windowsView = null, Type winPhoneView = null) where TView : Page where TViewModel : class @@ -100,18 +100,60 @@ namespace Prism.Autofac.Forms } } - /// <summary> - /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel - /// </summary> - /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> - /// <typeparam name="TViewModel">The shared ViewModel</typeparam> - /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> - /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> - /// <param name="desktopView">Desktop Specific View Type</param> - /// <param name="tabletView">Tablet Specific View Type</param> - /// <param name="phoneView">Phone Specific View Type</param> - /// <returns><see cref="IUnityContainer"/></returns> - public static IContainer RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + var name = typeof(TView).Name; + RegisterTypeForNavigationOnPlatform<TView, TViewModel>(container, name, platforms); + } + + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="name">The unique name to register with the Page. If left empty or null will default to the View name.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, string name, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + foreach (var platform in platforms) + { + if (Device.RuntimePlatform == platform.RuntimePlatform.ToString()) + { + container.RegisterTypeForNavigationWithViewModel<TViewModel>(platform.ViewType, name); + return; + } + } + + container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + + /// <summary> + /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">The shared ViewModel</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> + /// <param name="desktopView">Desktop Specific View Type</param> + /// <param name="tabletView">Tablet Specific View Type</param> + /// <param name="phoneView">Phone Specific View Type</param> + /// <returns><see cref="IUnityContainer"/></returns> + public static IContainer RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) where TView : Page where TViewModel : class { diff --git a/Source/Xamarin/Prism.DryIoc.Forms/DryIocExtensions.cs b/Source/Xamarin/Prism.DryIoc.Forms/DryIocExtensions.cs index d6aec29..edeb995 100644 --- a/Source/Xamarin/Prism.DryIoc.Forms/DryIocExtensions.cs +++ b/Source/Xamarin/Prism.DryIoc.Forms/DryIocExtensions.cs @@ -66,7 +66,7 @@ namespace Prism.DryIoc /// <param name="otherView">Other Platform Specific View Type</param> /// <param name="windowsView">Windows Specific View Type</param> /// <param name="winPhoneView">Windows Phone Specific View Type</param> - [Obsolete("RegisterTypeForNavigationOnPlatform is obsolete. Use switch(Device.RuntimePlatform) instead.")] + [Obsolete("This signature of the RegisterTypeForNavigationOnPlatform method is obsolete due to Device.OnPlatform being deprecated. Use the new IPlatform[] overload instead.")] public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, string name = null, Type androidView = null, Type iOSView = null, Type otherView = null, Type windowsView = null, Type winPhoneView = null) where TView : Page where TViewModel : class @@ -100,17 +100,59 @@ namespace Prism.DryIoc } } - /// <summary> - /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel - /// </summary> - /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> - /// <typeparam name="TViewModel">The shared ViewModel</typeparam> - /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> - /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> - /// <param name="desktopView">Desktop Specific View Type</param> - /// <param name="tabletView">Tablet Specific View Type</param> - /// <param name="phoneView">Phone Specific View Type</param> - public static void RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + var name = typeof(TView).Name; + RegisterTypeForNavigationOnPlatform<TView, TViewModel>(container, name, platforms); + } + + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="name">The unique name to register with the Page. If left empty or null will default to the View name.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IContainer container, string name, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + foreach (var platform in platforms) + { + if (Device.RuntimePlatform == platform.RuntimePlatform.ToString()) + { + container.RegisterTypeForNavigationWithViewModel<TViewModel>(platform.ViewType, name); + return; + } + } + + container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + + /// <summary> + /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">The shared ViewModel</typeparam> + /// <param name="container"><see cref="IContainer"/> used to register type for Navigation.</param> + /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> + /// <param name="desktopView">Desktop Specific View Type</param> + /// <param name="tabletView">Tablet Specific View Type</param> + /// <param name="phoneView">Phone Specific View Type</param> + public static void RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) where TView : Page where TViewModel : class { diff --git a/Source/Xamarin/Prism.Forms/Services/RuntimePlatform.cs b/Source/Xamarin/Prism.Forms/AppModel/RuntimePlatform.cs index 9700e84..f48741f 100644 --- a/Source/Xamarin/Prism.Forms/Services/RuntimePlatform.cs +++ b/Source/Xamarin/Prism.Forms/AppModel/RuntimePlatform.cs @@ -1,7 +1,7 @@ -namespace Prism.Services +namespace Prism.AppModel { /// <summary> - /// Represents tge Platform (OS) that the application is running on. + /// 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 diff --git a/Source/Xamarin/Prism.Forms/Platform{T}.cs b/Source/Xamarin/Prism.Forms/Platform{T}.cs new file mode 100644 index 0000000..aca96db --- /dev/null +++ b/Source/Xamarin/Prism.Forms/Platform{T}.cs @@ -0,0 +1,24 @@ +using Prism.AppModel; +using System; + +namespace Prism +{ + public interface IPlatform + { + RuntimePlatform RuntimePlatform { get; } + + Type ViewType { get; } + } + + public class Platform<TView> : IPlatform + { + public RuntimePlatform RuntimePlatform { get; } + + public Type ViewType { get { return typeof(TView); } } + + public Platform(RuntimePlatform runtimePlatform) + { + RuntimePlatform = runtimePlatform; + } + } +} diff --git a/Source/Xamarin/Prism.Forms/Prism.Forms.csproj b/Source/Xamarin/Prism.Forms/Prism.Forms.csproj index 2074cf6..7b7fd73 100644 --- a/Source/Xamarin/Prism.Forms/Prism.Forms.csproj +++ b/Source/Xamarin/Prism.Forms/Prism.Forms.csproj @@ -53,6 +53,7 @@ </PropertyGroup> <ItemGroup> <Compile Include="AppModel\IApplicationLifecycle.cs" /> + <Compile Include="AppModel\RuntimePlatform.cs" /> <Compile Include="Behaviors\BehaviorBase{T}.cs" /> <Compile Include="Behaviors\CarouselPageActiveAwareBehavior.cs" /> <Compile Include="Behaviors\NavigationPageSystemGoBackBehavior.cs" /> @@ -69,6 +70,7 @@ <Compile Include="Behaviors\EventToCommandBehavior.cs" /> <Compile Include="IPlatformInitializer.cs" /> <Compile Include="Navigation\INavigationPageOptions.cs" /> + <Compile Include="Platform{T}.cs" /> <Compile Include="PrismApplicationBase.cs" /> <Compile Include="Common\ApplicationProvider.cs" /> <Compile Include="Common\IApplicationProvider.cs" /> @@ -108,7 +110,6 @@ <Compile Include="AppModel\IApplicationStore.cs" /> <Compile Include="AppModel\ApplicationStore.cs" /> <Compile Include="FormsDependencyService.cs" /> - <Compile Include="Services\RuntimePlatform.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\Prism\Prism.csproj"> diff --git a/Source/Xamarin/Prism.Forms/Services/DeviceService.cs b/Source/Xamarin/Prism.Forms/Services/DeviceService.cs index 01f83e5..4b41b28 100644 --- a/Source/Xamarin/Prism.Forms/Services/DeviceService.cs +++ b/Source/Xamarin/Prism.Forms/Services/DeviceService.cs @@ -1,4 +1,5 @@ -using System; +using Prism.AppModel; +using System; using Xamarin.Forms; namespace Prism.Services diff --git a/Source/Xamarin/Prism.Forms/Services/IDeviceService.cs b/Source/Xamarin/Prism.Forms/Services/IDeviceService.cs index cbc92b5..80b3704 100644 --- a/Source/Xamarin/Prism.Forms/Services/IDeviceService.cs +++ b/Source/Xamarin/Prism.Forms/Services/IDeviceService.cs @@ -1,4 +1,5 @@ -using System; +using Prism.AppModel; +using System; using Xamarin.Forms; namespace Prism.Services diff --git a/Source/Xamarin/Prism.Ninject.Forms/NinjectExtensions.cs b/Source/Xamarin/Prism.Ninject.Forms/NinjectExtensions.cs index 9ed9206..c28438c 100644 --- a/Source/Xamarin/Prism.Ninject.Forms/NinjectExtensions.cs +++ b/Source/Xamarin/Prism.Ninject.Forms/NinjectExtensions.cs @@ -62,7 +62,7 @@ namespace Prism.Ninject /// <param name="otherView">Other Platform Specific View Type</param> /// <param name="windowsView">Windows Specific View Type</param> /// <param name="winPhoneView">Windows Phone Specific View Type</param> - [Obsolete("RegisterTypeForNavigationOnPlatform is obsolete. Use switch(Device.RuntimePlatform) instead.")] + [Obsolete("This signature of the RegisterTypeForNavigationOnPlatform method is obsolete due to Device.OnPlatform being deprecated. Use the new IPlatform[] overload instead.")] public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IKernel container, string name = null, Type androidView = null, Type iOSView = null, Type otherView = null, Type windowsView = null, Type winPhoneView = null) where TView : Page where TViewModel : class @@ -96,17 +96,59 @@ namespace Prism.Ninject } } - /// <summary> - /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel - /// </summary> - /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> - /// <typeparam name="TViewModel">The shared ViewModel</typeparam> - /// <param name="container"><see cref="IKernel"/> used to register type for Navigation.</param> - /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> - /// <param name="desktopView">Desktop Specific View Type</param> - /// <param name="tabletView">Tablet Specific View Type</param> - /// <param name="phoneView">Phone Specific View Type</param> - public static void RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IKernel container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IKernel"/> used to register type for Navigation.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IKernel container, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + var name = typeof(TView).Name; + RegisterTypeForNavigationOnPlatform<TView, TViewModel>(container, name, platforms); + } + + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IKernel"/> used to register type for Navigation.</param> + /// <param name="name">The unique name to register with the Page. If left empty or null will default to the View name.</param> + /// <param name="platforms"></param> + public static void RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IKernel container, string name, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + foreach (var platform in platforms) + { + if (Device.RuntimePlatform == platform.RuntimePlatform.ToString()) + { + container.RegisterTypeForNavigationWithViewModel<TViewModel>(platform.ViewType, name); + return; + } + } + + container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + + /// <summary> + /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">The shared ViewModel</typeparam> + /// <param name="container"><see cref="IKernel"/> used to register type for Navigation.</param> + /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> + /// <param name="desktopView">Desktop Specific View Type</param> + /// <param name="tabletView">Tablet Specific View Type</param> + /// <param name="phoneView">Phone Specific View Type</param> + public static void RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IKernel container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) where TView : Page where TViewModel : class { diff --git a/Source/Xamarin/Prism.Unity.Forms/UnityExtensions.cs b/Source/Xamarin/Prism.Unity.Forms/UnityExtensions.cs index 050e0f2..9bd7fe7 100644 --- a/Source/Xamarin/Prism.Unity.Forms/UnityExtensions.cs +++ b/Source/Xamarin/Prism.Unity.Forms/UnityExtensions.cs @@ -3,53 +3,55 @@ using Microsoft.Practices.Unity; using Xamarin.Forms; using Prism.Mvvm; using Prism.Navigation; +using Prism.Services; +using Prism.AppModel; namespace Prism.Unity { - public static class UnityExtensions - { - /// <summary> - /// Registers a Page for navigation. - /// </summary> - /// <typeparam name="TView">The Type of Page to register</typeparam> - /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> - /// <param name="name">The unique name to register with the Page</param> - public static IUnityContainer RegisterTypeForNavigation<TView>(this IUnityContainer container, string name = null) where TView : Page - { - var viewType = typeof(TView); - - if (string.IsNullOrWhiteSpace(name)) - name = viewType.Name; - - return container.RegisterTypeForNavigation(viewType, name); - } - - /// <summary> - /// Registers a Page for navigation - /// </summary> - /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> - /// <param name="viewType">The type of Page to register</param> - /// <param name="name">The unique name to register with the Page</param> - /// <returns><see cref="IUnityContainer"/></returns> - public static IUnityContainer RegisterTypeForNavigation(this IUnityContainer container, Type viewType, string name) - { - PageNavigationRegistry.Register(name, viewType); - return container.RegisterType(typeof(object), viewType, name); - } - - /// <summary> - /// Registers a Page for navigation. - /// </summary> - /// <typeparam name="TView">The Type of Page to register</typeparam> - /// <typeparam name="TViewModel">The ViewModel to use as the BindingContext for the Page</typeparam> - /// <param name="name">The unique name to register with the Page</param> - /// <param name="container"></param> - public static IUnityContainer RegisterTypeForNavigation<TView, TViewModel>(this IUnityContainer container, string name = null) - where TView : Page - where TViewModel : class - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(typeof(TView), name); - } + public static class UnityExtensions + { + /// <summary> + /// Registers a Page for navigation. + /// </summary> + /// <typeparam name="TView">The Type of Page to register</typeparam> + /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> + /// <param name="name">The unique name to register with the Page</param> + public static IUnityContainer RegisterTypeForNavigation<TView>(this IUnityContainer container, string name = null) where TView : Page + { + var viewType = typeof(TView); + + if (string.IsNullOrWhiteSpace(name)) + name = viewType.Name; + + return container.RegisterTypeForNavigation(viewType, name); + } + + /// <summary> + /// Registers a Page for navigation + /// </summary> + /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> + /// <param name="viewType">The type of Page to register</param> + /// <param name="name">The unique name to register with the Page</param> + /// <returns><see cref="IUnityContainer"/></returns> + public static IUnityContainer RegisterTypeForNavigation(this IUnityContainer container, Type viewType, string name) + { + PageNavigationRegistry.Register(name, viewType); + return container.RegisterType(typeof(object), viewType, name); + } + + /// <summary> + /// Registers a Page for navigation. + /// </summary> + /// <typeparam name="TView">The Type of Page to register</typeparam> + /// <typeparam name="TViewModel">The ViewModel to use as the BindingContext for the Page</typeparam> + /// <param name="name">The unique name to register with the Page</param> + /// <param name="container"></param> + public static IUnityContainer RegisterTypeForNavigation<TView, TViewModel>(this IUnityContainer container, string name = null) + where TView : Page + where TViewModel : class + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(typeof(TView), name); + } /// <summary> /// Registers a Page for navigation based on the current Device OS using a shared ViewModel @@ -64,85 +66,124 @@ namespace Prism.Unity /// <param name="windowsView">Windows Specific View Type</param> /// <param name="winPhoneView">Windows Phone Specific View Type</param> /// <returns><see cref="IUnityContainer"/></returns> - [Obsolete("RegisterTypeForNavigationOnPlatform is obsolete. Use switch(Device.RuntimePlatform) instead.")] + [Obsolete("This signature of the RegisterTypeForNavigationOnPlatform method is obsolete due to Device.OnPlatform being deprecated. Use the new IPlatform[] overload instead.")] public static IUnityContainer RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IUnityContainer container, string name = null, Type androidView = null, Type iOSView = null, Type otherView = null, Type windowsView = null, Type winPhoneView = null) - where TView : Page - where TViewModel : class - { - if (string.IsNullOrWhiteSpace(name)) - name = typeof(TView).Name; - - if (Device.OS == TargetPlatform.Android && androidView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(androidView, name); - } - else if (Device.OS == TargetPlatform.iOS && iOSView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(iOSView, name); - } - else if (Device.OS == TargetPlatform.Other && otherView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(otherView, name); - } - else if (Device.OS == TargetPlatform.Windows && windowsView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(windowsView, name); - } - else if (Device.OS == TargetPlatform.WinPhone && winPhoneView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(winPhoneView, name); - } - else - { - return container.RegisterTypeForNavigation<TView, TViewModel>(name); - } - } - - /// <summary> - /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel - /// </summary> - /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> - /// <typeparam name="TViewModel">The shared ViewModel</typeparam> - /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> - /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> - /// <param name="desktopView">Desktop Specific View Type</param> - /// <param name="tabletView">Tablet Specific View Type</param> - /// <param name="phoneView">Phone Specific View Type</param> - /// <returns><see cref="IUnityContainer"/></returns> - public static IUnityContainer RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IUnityContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) - where TView : Page - where TViewModel : class - { - if (string.IsNullOrWhiteSpace(name)) - name = typeof(TView).Name; - - if (Device.Idiom == TargetIdiom.Desktop && desktopView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(desktopView, name); - } - else if (Device.Idiom == TargetIdiom.Phone && phoneView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(phoneView, name); - } - else if (Device.Idiom == TargetIdiom.Tablet && tabletView != null) - { - return container.RegisterTypeForNavigationWithViewModel<TViewModel>(tabletView, name); - } - else - { - return container.RegisterTypeForNavigation<TView, TViewModel>(name); - } - } - - private static IUnityContainer RegisterTypeForNavigationWithViewModel<TViewModel>(this IUnityContainer container, Type viewType, string name) - where TViewModel : class - { - if (string.IsNullOrWhiteSpace(name)) - name = viewType.Name; - - ViewModelLocationProvider.Register(viewType.ToString(), typeof(TViewModel)); - - return container.RegisterTypeForNavigation(viewType, name); - } - } + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + if (Device.OS == TargetPlatform.Android && androidView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(androidView, name); + } + else if (Device.OS == TargetPlatform.iOS && iOSView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(iOSView, name); + } + else if (Device.OS == TargetPlatform.Other && otherView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(otherView, name); + } + else if (Device.OS == TargetPlatform.Windows && windowsView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(windowsView, name); + } + else if (Device.OS == TargetPlatform.WinPhone && winPhoneView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(winPhoneView, name); + } + else + { + return container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + } + + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> + /// <param name="platforms"></param> + public static IUnityContainer RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IUnityContainer container, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + var name = typeof(TView).Name; + return RegisterTypeForNavigationOnPlatform<TView, TViewModel>(container, name, platforms); + } + + /// <summary> + /// Registers a Page for navigation based on the current Device OS using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be shared across multiple Device Operating Systems if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">Shared ViewModel Type</typeparam> + /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> + /// <param name="name">The unique name to register with the Page. If left empty or null will default to the View name.</param> + /// <param name="platforms"></param> + public static IUnityContainer RegisterTypeForNavigationOnPlatform<TView, TViewModel>(this IUnityContainer container, string name, params IPlatform[] platforms) + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + foreach (var platform in platforms) + { + if (Device.RuntimePlatform == platform.RuntimePlatform.ToString()) + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(platform.ViewType, name); + } + + return container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + + /// <summary> + /// Registers a Page for navigation based on the Device Idiom using a shared ViewModel + /// </summary> + /// <typeparam name="TView">Default View Type to be used across multiple Idioms if they are not specified directly.</typeparam> + /// <typeparam name="TViewModel">The shared ViewModel</typeparam> + /// <param name="container"><see cref="IUnityContainer"/> used to register type for Navigation.</param> + /// <param name="name">The common name used for Navigation. If left empty or null will default to the ViewModel root name. i.e. MyPageViewModel => MyPage</param> + /// <param name="desktopView">Desktop Specific View Type</param> + /// <param name="tabletView">Tablet Specific View Type</param> + /// <param name="phoneView">Phone Specific View Type</param> + /// <returns><see cref="IUnityContainer"/></returns> + public static IUnityContainer RegisterTypeForNavigationOnIdiom<TView, TViewModel>(this IUnityContainer container, string name = null, Type desktopView = null, Type tabletView = null, Type phoneView = null) + where TView : Page + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = typeof(TView).Name; + + if (Device.Idiom == TargetIdiom.Desktop && desktopView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(desktopView, name); + } + else if (Device.Idiom == TargetIdiom.Phone && phoneView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(phoneView, name); + } + else if (Device.Idiom == TargetIdiom.Tablet && tabletView != null) + { + return container.RegisterTypeForNavigationWithViewModel<TViewModel>(tabletView, name); + } + else + { + return container.RegisterTypeForNavigation<TView, TViewModel>(name); + } + } + + private static IUnityContainer RegisterTypeForNavigationWithViewModel<TViewModel>(this IUnityContainer container, Type viewType, string name) + where TViewModel : class + { + if (string.IsNullOrWhiteSpace(name)) + name = viewType.Name; + + ViewModelLocationProvider.Register(viewType.ToString(), typeof(TViewModel)); + + return container.RegisterTypeForNavigation(viewType, name); + } + } } |