sthewissen / Xamarin.Forms.DebugRainbows

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

Setup through C# code behind? #13

Closed IeuanWalker closed 5 years ago

IeuanWalker commented 5 years ago

Hi,

I have create a custom ContentPage that acts as a base page for the majority of the pages in the app. I have create a property called IsDebug, so that any pages that is based on this content page can simply change the IsDebug to true and it will turn on the debug rainbow.

In the BasePage I have this - Xaml -

xmlns:rainbows="clr-namespace:Xamarin.Forms.DebugRainbows;assembly=Xamarin.Forms.DebugRainbows"
rainbows:DebugRainbow.IsDebug="False"

Code behind -

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BasePage : ContentPage
{
    public IList<View> BasePageContent => BaseContent.Children;

    public static readonly BindableProperty IsDebugProperty = BindableProperty.Create(nameof(IsDebug), typeof(bool), typeof(BasePage), false, defaultBindingMode: BindingMode.OneWay, propertyChanged: IsDebugChanged);
    public bool IsDebug
    {
        get => (bool)GetValue(IsDebugProperty);
        set => SetValue(IsDebugProperty, value);
    }
    public BasePage()
    {
        InitializeComponent();
    }

    private static void IsDebugChanged(BindableObject bindable, object oldValue, object newValue)
    {

        if (bindable != null && bindable is BasePage basePage)
        {
            // Enable or disable rainbow debug

        }
    }
}

Issue is im not sure how to set the value of rainbows:DebugRainbow.IsDebug="False" from the code behind.

sthewissen commented 5 years ago

Something like this should work?

DebugRainbow.SetIsDebug(basePage, (bool)newValue);

or

basePage.SetValue(DebugRainbow.IsDebugProperty, (bool)newValue);
IeuanWalker commented 5 years ago

@sthewissen perfect thanks :D