Ejemplo n.º 1
0
        private static TContent AddOrUpdateContentCache <TContent>(
            INavigationRegistration registration)

            where TContent : FrameworkElement
        {
            if (ContentCache.All(cachedPage => cachedPage.Type != registration.ViewType))
            {
                CreateCachedContent(registration);
            }

            var reference = ContentCache
                            .First(cachedPage => cachedPage.Type == registration.ViewType)
                            .Reference;

            if (!reference.TryGetTarget(out FrameworkElement view))
            {
                view = GetContentInstance <TContent>(
                    registration);

                reference.SetTarget(view);
            }


            return((TContent)view);
        }
        private static TPage AddOrUpdateContentCache <TPage>(
            INavigationRegistration registration)

            where TPage : Page
        {
            if (PageCache.All(cachedPage => cachedPage.Type != registration.ViewType))
            {
                CreateCachedPage(registration.ViewType);
            }

            var reference = PageCache
                            .First(cachedPage => cachedPage.Type == registration.ViewType)
                            .Reference;

            if (!reference.TryGetTarget(out Page view))
            {
                view = GetViewInstance <TPage>(
                    registration);

                reference.SetTarget(view);
            }


            return((TPage)view);
        }
Ejemplo n.º 3
0
        private static bool TryGetRegisteredViewModelType(
            Type viewType,
            out INavigationRegistration registration)
        {
            registration = NavigationRegistrations.FirstOrDefault(
                registration => registration.ViewType == viewType);


            return(registration != null);
        }
        /// <inheritdoc cref="CodeMonkeys.Navigation.INavigationService.Register(INavigationRegistration)" />
        public void Register(INavigationRegistration registration)
        {
            RegisterInternal(registration);

            if (registration.PreCreateInstance)
            {
                Task.Run(() => CreateCachedContent(registration));
            }

            Log?.Info($"Registered ViewModel of type {registration.ViewModelType.Name} to page {registration.ViewType.Name}.");
        }
Ejemplo n.º 5
0
        private static TView GetViewInstance <TView>(
            INavigationRegistration registrationInfo)

            where TView : Page
        {
            return(registrationInfo.ResolveViewUsingDependencyInjection
                ?
                   (TView)dependencyResolver.Resolve(
                       registrationInfo.ViewType)
                :
                   (TView)Activator.CreateInstance(
                       registrationInfo.ViewType));
        }
        private static void RemoveRegistration(
            INavigationRegistration registration)
        {
            var cachedPage = ContentCache.FirstOrDefault(
                cache => cache.Type == registration.ViewType);

            if (cachedPage != null)
            {
                ContentCache.Remove(cachedPage);
            }

            NavigationRegistrations.Remove(registration);

            Log?.Info($"Unregistered views from ViewModel of type {registration.ViewModelType.Name}.");
        }
        private static TView GetContentInstance <TView>(
            INavigationRegistration registration)

            where TView : FrameworkElement
        {
            if (registration.ResolveViewUsingDependencyInjection)
            {
                return((TView)dependencyResolver.Resolve(
                           registration.ViewType));
            }
            else
            {
                return((TView)Activator.CreateInstance(
                           registration.ViewType));
            }
        }
        private static bool TryGetRegistration(
            Type viewModelType,
            out INavigationRegistration registrationInfo)
        {
            if (!IsRegistered(viewModelType))
            {
                registrationInfo = null;
                return(false);
            }

            registrationInfo = NavigationRegistrations.OfType <RegistrationInfo>()
                               .FirstOrDefault(registration =>
                                               registration.ViewModelType == viewModelType);


            return(registrationInfo != null);
        }
        private static bool TryGetRegistration(
            Type viewModelType,
            Type viewType,
            out INavigationRegistration registrationInfo)
        {
            if (!IsRegistered(viewModelType, viewType))
            {
                registrationInfo = null;
                return(false);
            }

            registrationInfo = NavigationRegistrations.FirstOrDefault(registration =>
                                                                      registration.ViewModelType == viewModelType &&
                                                                      viewType.IsAssignableFrom(registration.ViewType) &&
                                                                      registration.Condition?.Invoke() != false);

            return(registrationInfo != null);
        }
Ejemplo n.º 10
0
        private static bool TryGetRegistration(
            Type viewModelInterfaceType,
            out INavigationRegistration registrationInfo)
        {
            if (!IsRegistered(viewModelInterfaceType))
            {
                registrationInfo = null;
                return(false);
            }


            registrationInfo = NavigationRegistrations.OfType <NavigationRegistration>()
                               .FirstOrDefault(registration =>
                                               registration.ViewModelType == viewModelInterfaceType &&
                                               registration.Platform.ToXamarinPlatform() == Device.RuntimePlatform);


            return(registrationInfo != null);
        }
Ejemplo n.º 11
0
        private static void CreateCachedContent(
            INavigationRegistration registration)
        {
            if (!Configuration.CacheContent)
            {
                return;
            }

            if (Configuration.ContentTypesToExcludeFromCaching?
                .Contains(registration.ViewType) == true)
            {
                return;
            }

            var content = GetContentInstance <FrameworkElement>(
                registration);

            var cachedContent = new CachedContent(content);

            ContentCache.Add(cachedContent);
        }
Ejemplo n.º 12
0
        /// <inheritdoc cref="CodeMonkeys.Navigation.INavigationService.Register(INavigationRegistration)" />
        public void Register(
            INavigationRegistration registrationInfo)
        {
            if (registrationInfo is NavigationRegistration xamarinRegistration)
            {
                if (xamarinRegistration.Platform != DevicePlatforms.All &&
                    Device.RuntimePlatform.ToDevicePlatform() != xamarinRegistration.Platform)
                {
                    return;
                }
            }

            RegisterInternal(registrationInfo);

            if (registrationInfo.PreCreateInstance)
            {
                Task.Run(() => CreateCachedPage(registrationInfo.ViewType));
            }

            Log?.Info($"Registered ViewModel of type {registrationInfo.ViewModelType.Name} to page {registrationInfo.ViewType.Name}.");
        }
Ejemplo n.º 13
0
        private static void RegisterInternal(
            INavigationRegistration registration)
        {
            _semaphore.Wait();

            try
            {
                if (TryGetRegistration(
                        registration.ViewModelType,
                        registration.ViewType,
                        out var registrationInfo))
                {
                    NavigationRegistrations.Remove(registrationInfo);
                }

                NavigationRegistrations.Add(registration);
            }
            finally
            {
                _semaphore.Release();
            }
        }
        private static void RegisterInternal(
            INavigationRegistration registration)
        {
            _semaphore.Wait();

            try
            {
                if (!Configuration.AllowDifferentViewTypeRegistrationForSameViewModel &&
                    TryGetRegistration(
                        registration.ViewModelType,
                        out var registrationInfo))
                {
                    NavigationRegistrations.Remove(registrationInfo);
                }

                NavigationRegistrations.Add(registration);
            }
            finally
            {
                _semaphore.Release();
            }
        }