Example #1
0
        // For most of the UWP controls, this custom VisualStateManager is injected to prevent the default Windows
        // VSM from handling states which the Forms VSM is already managing.
        // The exception are the controls which are built on TextBox (SearchBar, Entry, Editor); there's a UWP
        // bug wherein the GoToStateCore override for those controls is never called (Item 10976357 in VSTS)
        // So until that's resolved, the FormsTextBox control is doing that work as best it can.

        protected override bool GoToStateCore(Control control, FrameworkElement templateRoot, string stateName,
                                              global::Windows.UI.Xaml.VisualStateGroup @group, global::Windows.UI.Xaml.VisualState state, bool useTransitions)
        {
            // If this custom VSM is in play, it's because the control is using the Forms VSM or the user has disabled
            // legacy color handling. Either way, we may need to prevent the Windows VSM from transitioning to the new
            // state. So we intercept the transition here.

            if (ShouldIntercept(control, state, stateName))
            {
                return(false);
            }

            return(base.GoToStateCore(control, templateRoot, stateName, @group, state, useTransitions));
        }
Example #2
0
        VisualStateCache PseudoDisable(Control control)
        {
            if (VisualTreeHelper.GetChildrenCount(control) == 0)
            {
                control.ApplyTemplate();
            }

            WVisualStateManager.GoToState(control, "Disabled", true);

            var rootElement = (FrameworkElement)VisualTreeHelper.GetChild(control, 0);

            var cache = new VisualStateCache();
            IList <WVisualStateGroup> groups = WVisualStateManager.GetVisualStateGroups(rootElement);

            WVisualStateGroup common = null;

            foreach (var group in groups)
            {
                if (group.Name == "CommonStates")
                {
                    common = group;
                }
                else if (group.Name == "FocusStates")
                {
                    cache.FocusStates = group;
                }
                else if (cache.FocusStates != null && common != null)
                {
                    break;
                }
            }

            if (cache.FocusStates != null)
            {
                groups.Remove(cache.FocusStates);
            }

            if (common != null)
            {
                foreach (WVisualState state in common.States)
                {
                    if (state.Name == "Normal")
                    {
                        cache.Normal = state;
                    }
                    else if (state.Name == "Pressed")
                    {
                        cache.Pressed = state;
                    }
                    else if (state.Name == "PointerOver")
                    {
                        cache.PointerOver = state;
                    }
                }

                if (cache.Normal != null)
                {
                    common.States.Remove(cache.Normal);
                }
                if (cache.Pressed != null)
                {
                    common.States.Remove(cache.Pressed);
                }
                if (cache.PointerOver != null)
                {
                    common.States.Remove(cache.PointerOver);
                }
            }

            return(cache);
        }
Example #3
0
        void UpdateOnColor()
        {
            if (!(Cell is SwitchCell switchCell))
            {
                return;
            }

            var color = switchCell.OnColor == Color.Default
                                ? _defaultOnColor
                                : new SolidColorBrush(switchCell.OnColor.ToWindowsColor());

            var nativeSwitch = FrameworkElementExtensions.GetFirstDescendant <ToggleSwitch>(this);

            // change fill color in switch rectangle
            var rects = nativeSwitch.GetDescendantsByName <global::Windows.UI.Xaml.Shapes.Rectangle>("SwitchKnobBounds");

            foreach (var rect in rects)
            {
                rect.Fill = color;
            }

            // change color in animation on PointerOver
            var grid = nativeSwitch.GetFirstDescendant <global::Windows.UI.Xaml.Controls.Grid>();
            var gridVisualStateGroups = global::Windows.UI.Xaml.VisualStateManager.GetVisualStateGroups(grid);

            global::Windows.UI.Xaml.VisualStateGroup vsGroup = null;
            foreach (var visualGroup in gridVisualStateGroups)
            {
                if (visualGroup.Name == "CommonStates")
                {
                    vsGroup = visualGroup;
                    break;
                }
            }
            if (vsGroup == null)
            {
                return;
            }

            global::Windows.UI.Xaml.VisualState vState = null;
            foreach (var visualState in vsGroup.States)
            {
                if (visualState.Name == "PointerOver")
                {
                    vState = visualState;
                    break;
                }
            }
            if (vState == null)
            {
                return;
            }

            var visualStates = vState.Storyboard.Children;

            foreach (ObjectAnimationUsingKeyFrames item in visualStates)
            {
                if ((string)item.GetValue(Storyboard.TargetNameProperty) == "SwitchKnobBounds")
                {
                    item.KeyFrames[0].Value = color;
                    break;
                }
            }
        }