Xamarin 应用程序在启用暗模式的 Android 设备上自动切换到暗模式 [英] Xamarin app automatically switching to dark mode on Android device with dark mode on

查看:26
本文介绍了Xamarin 应用程序在启用暗模式的 Android 设备上自动切换到暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Xamarin.Forms 应用程序(Shell 项目)在启用了深色主题的 Android 手机上会自动切换到深色主题.我不希望这种情况发生.我尝试了多种方法来禁用它,但都没有奏效.知道出了什么问题吗?

my Xamarin.Forms app (Shell project) keeps automatically switching to dark theme when on an Android phone with dark theme enabled. I don't want this to happen. I tried multiple ways to disable this, but none of them worked. Any idea what's wrong?

AppShell.xaml 中有趣的代码部分是:

The interesting part of the code in AppShell.xaml is:

<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       xmlns:retrogamez="clr-namespace:RetroGameZ"
       Title="RetroGameZ"
       x:Class="RetroGameZ.AppShell">
       

    <Shell.Resources>
        <ResourceDictionary>
            <Style x:Key="BaseStyle" TargetType="Element">
                <Setter Property="Shell.BackgroundColor" Value="#049DBF" />
                <Setter Property="Shell.ForegroundColor" Value="White" />
                <Setter Property="Shell.TitleColor" Value="White" />
                <Setter Property="Shell.DisabledColor" Value="#03A6A6" />
                <Setter Property="Shell.UnselectedColor" Value="#D3D3D3" />
                <Setter Property="Shell.TabBarBackgroundColor" Value="#049DBF" />
                <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                <Setter Property="Shell.TabBarUnselectedColor" Value="#D3D3D3"/>
                <Setter Property="Shell.TabBarTitleColor" Value="White"/>
            </Style>
            <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
        </ResourceDictionary>
    </Shell.Resources>

后来只有单独的组件.

推荐答案

找到了适用于 Android 的解决方案:

Found the solution for Android:

MainActivity.cs中,在base.OnCreate()之前,添加这一行:

In MainActivity.cs, before base.OnCreate(), add this line:

AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

这篇关于Xamarin 应用程序在启用暗模式的 Android 设备上自动切换到暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆