sthewissen / Xamarin.Forms.DebugRainbows

The package you didn't even know you needed!
MIT License
415 stars 33 forks source link

Enable/Disable DebugRainbows at runtime #30

Closed ericbrunner closed 3 years ago

ericbrunner commented 3 years ago

Is there any way to enable or disable DebugRainbows for all ContentPages and ContentViews at runtime. I tried to use that approach with MergedDictionaries like in that sample:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/theming

but it doesn't work- I created a Debug.xaml / Debug.xaml.cs and a NoDebug.xaml & NoDebug.xaml.cs like the Dark and Light theme in the sample. Then I used similar code to switch the Debug and NoDebug ResourceDictionary. But no effect. Ah to mention. In Debug.xaml I set Value to true, in NoDebug.xaml to false like

Debug

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
               <Setter Property="rainbows:DebugRainbow.ShowColors" Value="True" />
            </Style>

NoDebug

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
               <Setter Property="rainbows:DebugRainbow.ShowColors" Value="False" />
            </Style>

Thanks in advance if you have a running sample or a hint if it's possible to do the switch at runtime.

sthewissen commented 3 years ago

I'm not sure if it's possible, to be honest. The coloring is applied on page creation, so unless a new layout pass is performed the colors will stay. When it comes to Hot Reload (which this was kind of designed for) that works like a charm. When changing the boolean at runtime the pages that are already created will probably remain colored. Do new pages you spawn from that point onwards still get the color as well?

ericbrunner commented 3 years ago

The app is a lecacy app which started back in 2014 as fully code-based UI (no XAML). There is only one ContentPage. In that ContentPagethere is a Gridwhere various ContentViews are set at runtime. So to catch up all ContentView's ,too I added them as Style Setter like:

            void EnableDebugRainbows(bool shouldUseDebugRainbows)
            {
                Resources.Add(new Style(typeof(ContentPage))
                {
                    ApplyToDerivedTypes = true,
                    Setters = {
                        new Setter
                        {
                            Property = Xamarin.Forms.DebugRainbows.DebugRainbow.ShowColorsProperty,
                            Value = shouldUseDebugRainbows
                        }
                    }
                });

                Resources.Add(new Style(typeof(ContentView))
                {
                    ApplyToDerivedTypes = true,
                    Setters = {
                        new Setter
                        {
                            Property = Xamarin.Forms.DebugRainbows.DebugRainbow.ShowColorsProperty,
                            Value = shouldUseDebugRainbows
                        }
                    }
                });
            }

            EnableDebugRainbows(true);

Well the blue frame is the ContentView's Area in my Gridbut the Content is not shown :-(

image