/// <summary>
        /// Tries to match the selector with a control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="subscribe">
        /// Whether the match should subscribe to changes in order to track the match over time,
        /// or simply return an immediate result.
        /// </param>
        /// <returns>A <see cref="SelectorMatch"/>.</returns>
        public SelectorMatch Match(IStyleable control, bool subscribe = true)
        {
            List <IObservable <bool> > inputs = new List <IObservable <bool> >();
            Selector selector = this;

            while (selector != null)
            {
                if (selector.InTemplate && control.TemplatedParent == null)
                {
                    return(SelectorMatch.False);
                }

                var match = selector.Evaluate(control, subscribe);

                if (match.ImmediateResult == false)
                {
                    return(match);
                }
                else if (match.ObservableResult != null)
                {
                    inputs.Add(match.ObservableResult);
                }

                selector = selector.MovePrevious();
            }

            if (inputs.Count > 0)
            {
                return(new SelectorMatch(StyleActivator.And(inputs)));
            }
            else
            {
                return(SelectorMatch.True);
            }
        }
示例#2
0
        /// <summary>
        /// Tries to match the selector with a control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="subscribe">
        /// Whether the match should subscribe to changes in order to track the match over time,
        /// or simply return an immediate result.
        /// </param>
        /// <returns>A <see cref="SelectorMatch"/>.</returns>
        public SelectorMatch Match(IStyleable control, bool subscribe = true)
        {
            ValueSingleOrList <IObservable <bool> > inputs = default;

            var selector       = this;
            var alwaysThisType = true;
            var hitCombinator  = false;

            while (selector != null)
            {
                hitCombinator |= selector.IsCombinator;

                var match = selector.Evaluate(control, subscribe);

                if (!match.IsMatch)
                {
                    return(hitCombinator ? SelectorMatch.NeverThisInstance : match);
                }
                else if (selector.InTemplate && control.TemplatedParent == null)
                {
                    return(SelectorMatch.NeverThisInstance);
                }
                else if (match.Result == SelectorMatchResult.AlwaysThisInstance)
                {
                    alwaysThisType = false;
                }
                else if (match.Result == SelectorMatchResult.Sometimes)
                {
                    Debug.Assert(match.Activator != null);

                    inputs.Add(match.Activator);
                }

                selector = selector.MovePrevious();
            }

            if (inputs.HasList)
            {
                return(new SelectorMatch(StyleActivator.And(inputs.List)));
            }
            else if (inputs.IsSingle)
            {
                return(new SelectorMatch(inputs.Single));
            }
            else
            {
                return(alwaysThisType && !hitCombinator ?
                       SelectorMatch.AlwaysThisType :
                       SelectorMatch.AlwaysThisInstance);
            }
        }