【问题标题】:How can programmatically change from Light Mode to Dark Mode in Xamarin.Forms Android and / or programmatically change the color of the scroll bar?如何在 Xamarin.Forms Android 中以编程方式从 Light Mode 更改为 Dark Mode 和/或以编程方式更改滚动条的颜色?
【发布时间】:2020-04-11 11:47:49
【问题描述】:

我想让我的用户根据需要选择 AppTheme。以下是我想给出的选项:-

  1. 自动(根据用户在设置中的选择)。
  2. 灯光模式
  3. 深色模式

我找到了使用以下代码的解决方案:-

        if(theme == App.Theme.Light)
        {
            Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightNo);
        } else
        {
            Delegate.SetLocalNightMode(AppCompatDelegate.ModeNightYes);
        }

但是这段代码重新创建了整个活动,并且在单击按钮时,我再次被带到我的应用程序的登录页面。

谁能建议我在 Xamarin.Forms Android 中以编程方式从浅色模式更改为深色模式?

还有谁知道我可以以编程方式更改滚动条颜色的另一种方法。

【问题讨论】:

  • 等待XF4.6 它将有更好的暗模式支持
  • 顺便说一句,滚动条颜色变化是什么意思,您的代码不显示任何滚动视图。您是指状态栏吗?

标签: xamarin xamarin.forms


【解决方案1】:

有两个样式文件 Light 和 Dark XAML 和 ThemeHelper 类,在运行时切换主题

参考这个https://github.com/jamesmontemagno/Hanselman.Forms/tree/vnext/src/Hanselman/Styles

更新

如前所述,此示例项目具有大量用于主题切换的资源。

轻主题

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TestApp.LightTheme">
    <Color
        x:Key="TextColor">#ababab</Color>
    <Color
        x:Key="GenericBackground">#e3e3e3</Color>
    <Color
        x:Key="AppBackground">#FFFFFF</Color>
</ResourceDictionary>

深色主题

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TestApp.DarkTheme">
    <Color
        x:Key="TextColor">#e3e3e3</Color>
    <Color
        x:Key="GenericBackground">#ababab</Color>
    <Color
        x:Key="AppBackground">#000000</Color>
</ResourceDictionary>

主题助手

public class ThemeHelper
{

    public static Theme CurrentTheme = Theme.Light;

    public static void ChangeTheme(Theme theme, bool forceTheme = false)
    {
        // don't change to the same theme
        if (theme == CurrentTheme && !forceTheme)
            return;

        //// clear all the resources
        var applicationResourceDictionary = Application.Current.Resources;
        ResourceDictionary newTheme;
        if (theme == Theme.Default)
        {

            theme = AppInfo.RequestedTheme == AppTheme.Dark ? Theme.Dark : Theme.Light;
        }

        switch (theme)
        {
            case Theme.Light:
                newTheme = new LightTheme();
                break;
            case Theme.Dark:
                newTheme = new DarkTheme();
                break;
            case Theme.Default:
            default:
                newTheme = new LightTheme();
                break;
        }

        ManuallyCopyThemes(newTheme, applicationResourceDictionary);

        CurrentTheme = theme;

        var background = (Color)App.Current.Resources["AppBackground"];

    }

    static void ManuallyCopyThemes(ResourceDictionary fromResource, ResourceDictionary toResource)
    {
        foreach (var item in fromResource.Keys)
        {
            toResource[item] = fromResource[item];
        }
    }
}

主题枚举

public enum Theme
{
    Default,
    Light,
    Dark
}

在启动应用程序初始化主题之前,您可以从应用程序设置或 SQLite 或 API 等数据库中检索它

public App()
{
    InitializeComponent();
    ThemeHelper.ChangeTheme(Theme.Default);
    MainPage = new NavigationPage(new MainPage());
}

确保在这种方法中使用DynamicResource

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    BackgroundColor="{DynamicResource AppBackground}"
    x:Class="TestApp.MainPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem
            Text="Default"
            Clicked="ToolbarItem_Clicked" />
        <ToolbarItem
            Text="Light"
            Clicked="ToolbarItem_Clicked_1" />
        <ToolbarItem
            Text="Dark"
            Clicked="ToolbarItem_Clicked_2" />
    </ContentPage.ToolbarItems>
    <StackLayout>
        <Label
            Text="Welcome to Xamarin.Forms!"
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand"
            TextColor="{DynamicResource TextColor}"
            BackgroundColor="{DynamicResource GenericBackground}" />
        <Label
            x:Name="label"
            TextColor="{DynamicResource TextColor}"
            BackgroundColor="{DynamicResource GenericBackground}"
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

截图

您可能仍需要在 Android 中针对不同的 API 级别进行一些更改。

其他参考

Theming Dark mode detection

Android Dark Theme

【讨论】:

    【解决方案2】:

    您可以使用 DynamicResources 来实现动态主题功能而无需重定向。你可以关注these documents

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多