/// <summary> /// Registers the known services with the ServiceLocator type. /// </summary> /// <param name="defaultLocator">ServiceLocator, if null, DependencyService is used.</param> /// <param name="registerBehavior">Registration behavior</param> /// <returns>IDependencyService</returns> public static IDependencyService Init(IDependencyService defaultLocator, RegisterBehavior registerBehavior) { // If the ServiceLocator has already been set, then something used it before // Init was called. This is not allowed if they are going to change the locator. if (defaultLocator != null && serviceLocator != null) { throw new InvalidOperationException( $"Must call {nameof(XamUInfrastructure.Init)} before using any library features; " + "ServiceLocator has already been set."); } // Assign the locator; either use the supplied one, or the default // DependencyService version if not supplied. if (defaultLocator == null) { defaultLocator = ServiceLocator; } else { Debug.Assert(serviceLocator == null); serviceLocator = defaultLocator; } // Register the services if ((registerBehavior & RegisterBehavior.MessageVisualizer) != 0) { defaultLocator.Register <IMessageVisualizerService, FormsMessageVisualizerService>(); } if ((registerBehavior & RegisterBehavior.Navigation) != 0) { // Use a single instance for the navigation service and // register both interfaces against it. var navService = new FormsNavigationPageService(); defaultLocator.Register <INavigationPageService>(navService); defaultLocator.Register <INavigationService>(navService); } defaultLocator.Register <IDependencyService>(defaultLocator); return(defaultLocator); }
/// <summary> /// Registers the known services with the ServiceLocator type. /// </summary> /// <param name="defaultLocator">ServiceLocator, if null, DependencyService is used.</param> /// <param name="registerBehavior">Registration behavior</param> /// <returns>IDependencyService</returns> public static IDependencyService Init(IDependencyService defaultLocator, RegisterBehavior registerBehavior) { // If the ServiceLocator has already been set, then something used it before // Init was called. This is not allowed if they are going to change the locator. if (defaultLocator != null && serviceLocator != null) { throw new InvalidOperationException( $"Must call {nameof(XamUInfrastructure.Init)} before using any library features; " + "ServiceLocator has already been set."); } // Can call Init multiple times as long as you don't change the locator. if (initialized) { Debug.Assert(serviceLocator != null); return(ServiceLocator); } // Only do the remaining logic once or we get duplicate key exceptions. initialized = true; // Assign the locator; either use the supplied one, or the default // DependencyService version if not supplied. if (defaultLocator == null) { defaultLocator = ServiceLocator; } else { Debug.Assert(serviceLocator == null); serviceLocator = defaultLocator; } // Register the services if ((registerBehavior & RegisterBehavior.MessageVisualizer) == RegisterBehavior.MessageVisualizer) { try { defaultLocator.Register <IMessageVisualizerService, FormsMessageVisualizerService>(); } catch (ArgumentException) { } } if ((registerBehavior & RegisterBehavior.Navigation) == RegisterBehavior.Navigation) { // Use a single instance for the navigation service and // register both interfaces against it. var navService = new FormsNavigationPageService(); try { defaultLocator.Register <INavigationPageService>(navService); defaultLocator.Register <INavigationService>(navService); } catch (ArgumentException) { } } try { defaultLocator.Register <IDependencyService>(defaultLocator); } catch (ArgumentException) { } return(defaultLocator); }