6

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

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

<?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>

后来只有个别组件。

4

3 回答 3

9

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

MainActivity.cs, beforebase.OnCreate()中,添加以下行:

AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
于 2021-03-16T17:03:42.127 回答
3

我尝试了上述所有解决方案,但只有以下步骤对我有用。

  1. 打开MainActivity.cs并添加AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;as 方法的第一行OnCreate
protected override void OnCreate(Bundle savedInstanceState)
{
    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

    base.OnCreate(savedInstanceState);
   // ... other Xamarin stuff
}
  1. 打开Resources/values/styles.xml并添加以下行<item name="android:forceDarkAllowed">false</item>
<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="MainTheme.Base">
    <!-- ... other Xamarin stuff -->
    <item name="android:forceDarkAllowed">false</item>
    <!-- ... other Xamarin stuff -->
  </style>
</resources>

参考:https ://stackoverflow.com/a/64339016/6846888

于 2021-08-02T13:57:39.617 回答
0

假设您正在使用 Xamarin.Forms 嵌入功能来处理https://devblogs.microsoft.com/xamarin/app-themes-xamarin-forms/中提到的主题样式。

尝试在您的App.cs

App.Current.UserAppTheme = OSAppTheme.Light;

如果你没有设置任何东西或者你设置了

App.Current.UserAppTheme = OSAppTheme.Unspecified;

它将遵循您的操作系统当前主题。

于 2020-11-09T17:08:16.133 回答