Replacing Xamarin.Forms.Theme.Dark in develoments made with latest versions of Xamarin Forms

201 Views Asked by At

Xamarin.Forms.Theme.Dark was a nice way to set a dark look to an Xamarin Forms application (Android & iOS). Obviously, this project is no longer supported. Although it still works with version 4.2, I am looking for a replacement solution. As far as I understand the new "Theming" possibililities of Xamarin Forms, the theme has to be consumed in the XAML code on each object. Is there still a solution to transform the style at "application level" ?

1

There are 1 best solutions below

0
AudioBubble On

This code answers the above request:

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace M...
{
    internal static class DarkTheme
    {
        internal static ResourceDictionary getResources()
        {
            var ret = new ResourceDictionary();

            ret.Add(new Style(typeof(TemplatedPage))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = { new Setter { Property = TemplatedPage.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.1882353, 0.1882353, 0.1882353, 1.0), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } }
            });

            ret.Add(new Style(typeof(NavigationPage))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = NavigationPage.BarTextColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
                    new Setter { Property = NavigationPage.BarBackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12941177, 0.12941177, 0.12941177, 1), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.TabbedPage))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = TabbedPage.BarTextColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
                    new Setter { Property = TabbedPage.BarBackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12941177, 0.12941177, 0.12941177, 1), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.Button))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = Button.FontSizeProperty, Value = iifPlatform<int>(16, 18) } ,
                    new Setter { Property = Button.FontAttributesProperty, Value = iifPlatform<int>(0, 1) } ,
                    new Setter { Property = Button.CornerRadiusProperty, Value = iifPlatform<int>(0, 6) } ,
                    new Setter { Property = Button.BorderWidthProperty, Value = 0 } ,
                    new Setter { Property = Button.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.46666667, 0.46666667, 0.46666667, 1.0), Color.FromRgba(1.0, 1.0, 1.0, 0d)) } ,
                    new Setter { Property = Button.BorderColorProperty, Value = Color.FromRgba(0.2, 0.2, 0.2, 1.0) } ,
                    new Setter { Property = Button.TextColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0, 0.47843137, 1.0, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.Entry))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = Entry.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
                    new Setter { Property = Entry.PlaceholderColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
                    new Setter { Property = Entry.BackgroundColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0.2509804, 0.2509804, 0.2509804, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.Editor))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = Editor.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
                    new Setter { Property = Editor.BackgroundColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0.2509804, 0.2509804, 0.2509804, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.Frame))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = Frame.BorderColorProperty, Value = Color.FromRgba(0.28627452, 0.28627452, 0.28627452, 1.0) } ,
                    new Setter { Property = Frame.HasShadowProperty, Value = false } ,
                    new Setter { Property = Frame.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.92941177, 0.92941177, 0.93333334, 1), Color.FromRgba(0.12156863, 0.12156863, 0.12156863, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.SearchBar))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = { new Setter { Property = SearchBar.TextColorProperty, Value = Color.FromRgba(1.0, 0d, 0d, 1.0) } }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.Label))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = Label.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
                    new Setter { Property = Label.FontSizeProperty, Value = 15 } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.ImageCell))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = ImageCell.TextColorProperty, Value = Color.FromRgba(1.0, 1.0, 1.0, 1.0) } ,
                    new Setter { Property = ImageCell.DetailColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0, 0.47843137, 1.0, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.TextCell))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = TextCell.TextColorProperty, Value = Color.FromRgba(1.0, 1.0, 1.0, 1.0) } ,
                    new Setter { Property = TextCell.DetailColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.EntryCell))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = { new Setter { Property = EntryCell.LabelColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.ListView))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = {
                    new Setter { Property = ListView.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 0d), Color.FromRgba(0.10980392, 0.10980392, 0.11372549, 1)) } ,
                    new Setter { Property = ListView.SeparatorColorProperty, Value = Color.FromRgba(0.28627452, 0.28627452, 0.28627452, 1.0) } ,
                    new Setter { Property = ListView.SeparatorVisibilityProperty, Value = 0 } ,
                }
            });

            ret.Add(new Style(typeof(Xamarin.Forms.ActivityIndicator))
            {
                ApplyToDerivedTypes = true,
                CanCascade = true,
                Setters = { new Setter { Property = ActivityIndicator.ColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0d, 0d, 0d, 0d), Color.FromRgba(0d, 0.47843137, 0d, 1.0)) } }
            });

            return ret;
        }


        private static T iifPlatform<T>(T androidValue, T iosValue)
        {
            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    return iosValue;
                default:
                    return androidValue;
            }
        }
    }
}

It should be invoked in App.xaml.cs with this line of code :Resources = DarkTheme.getResources();

This does not include all the content of Xamarin.Forms.Theme.Dark.dll but the black styled rendering is identical.