Exemple #1
0
        protected TService GetService(object targetObject, DependencyProperty resolverServiceProperty)
        {
            if (targetObject == null)
            {
                throw new ArgumentNullException(nameof(targetObject));
            }
            if (resolverServiceProperty == null)
            {
                throw new ArgumentNullException(nameof(resolverServiceProperty));
            }

            switch (ResolverSource)
            {
            case ResolverServiceSource.Global:
                return(GlobalServices.Get <TService>());

            case ResolverServiceSource.Ancestor:
                if (targetObject is DependencyObject dependencyObject)
                {
                    var viewRegistryServiceTraverse = dependencyObject.Traverse(o => o.GetVisualParent(), o => o.GetValue(resolverServiceProperty) == null);
                    return(viewRegistryServiceTraverse.LastOrDefault()?.GetValue(resolverServiceProperty) as TService);
                }

                throw new InvalidOperationException("Ancestor resolver source available only for DependencyObjects");

            case ResolverServiceSource.Direct:
                return(Resolver);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #2
0
        protected BaseResolver()
        {
            var globalService = GlobalServices.Get <TService>();

            ResolverSource = globalService == null ? ResolverServiceSource.Ancestor : ResolverServiceSource.Global;
        }