コード例 #1
0
ファイル: CellControl.cs プロジェクト: pjcollins/maui
 void SetDefaultSwitchColor()
 {
     if (_defaultOnColor == null && Cell is SwitchCell)
     {
         var nativeSwitch = FrameworkElementExtensions.GetFirstDescendant <ToggleSwitch>(this);
         var rects        = nativeSwitch.GetDescendantsByName <Microsoft.UI.Xaml.Shapes.Rectangle>("SwitchKnobBounds");
         foreach (var rect in rects)
         {
             _defaultOnColor = rect.Fill;
         }
         UpdateOnColor();
     }
 }
コード例 #2
0
ファイル: CellControl.cs プロジェクト: pjcollins/maui
        void UpdateOnColor()
        {
            if (!(Cell is SwitchCell switchCell))
            {
                return;
            }

            var color = switchCell.OnColor.IsDefault()
                                ? _defaultOnColor
                                : new WSolidColorBrush(switchCell.OnColor.ToWindowsColor());

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

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

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

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

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

            Microsoft.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;
                }
            }
        }