首页 > 解决方案 > Android 上的全局样式问题,iOS 工作正常(Xamarin)

问题描述

我在 Xamarin 应用程序中遇到了一些奇怪的行为。在共享项目中,我将所有基于控件的样式分隔在单个 xaml 文件中。

在此处输入图像描述

所有样式都组合在DefaultTheme.xaml文件中。

在此处输入图像描述

然后, DefaultTheme.xaml 将被LightTheme.xamland使用DarkTheme.xaml

在此处输入图像描述

我的风格就是这样管理的。然后,根据“Darkomde on/off”,我可以在 iOS 和 Android 项目中加载LightTheme.xaml或。DarkTheme.xaml

在 Android 上,我在 MainActivity.cs 中设置了这样的主题(我也在使用 SplashActivity.cs,应该已经设置了吗?)

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    LoadApplication(new App());

    SetAppTheme();
}

void SetAppTheme()
{
    if (Resources.Configuration.UiMode.HasFlag(UiMode.NightYes))
        SetTheme(RemoteControlRepetierServer.Theme.Dark);
    else
        SetTheme(RemoteControlRepetierServer.Theme.Light);
}

public void SetTheme(Theme theme)
{
    switch (theme)
    {
        case RemoteControlRepetierServer.Theme.Light:
            // Night mode is not active, we're using the light theme
            if (App.AppTheme == "light")
                return;
            App.Current.Resources = new LightTheme();
            App.AppTheme = "light";

            break;
        case RemoteControlRepetierServer.Theme.Dark:
            // Night mode is active, we're using dark theme
            if (App.AppTheme == "dark")
                return;
            //Add a Check for App Theme since this is called even when not changed really
            App.Current.Resources = new DarkTheme();
            App.AppTheme = "dark";
            break;
    }
    App.CurrentAppTheme = theme;
}

到目前为止,一切都很好。现在,如果我启动我的 Android 应用程序,似乎并没有加载所有样式。例如Gauge. 虽然第一个选项卡上的仪表看起来应该如此,但第二个选项卡上的仪表却没有。

仪表正常 仪表不正常

两者都使用相同的 XAML 进行样式设置。

<gauge:Scale.Pointers>
    <gauge:NeedlePointer Value="{Binding SelectedExtruder.TempRead}" Color="{DynamicResource Gray-Black}" 
                        KnobColor="{DynamicResource Gray-Black}" KnobStrokeColor="{DynamicResource Gray-Black}"/>
    <gauge:RangePointer Value="{Binding SelectedExtruder.TempRead}" Color="{DynamicResource PrimaryColor}"
                    RangeCap="Both"
                    />
    <gauge:MarkerPointer Value="{Binding SetExtruderTemp}" Color="{DynamicResource Red}"/>
</gauge:Scale.Pointers>

也没有在第二个选项卡上设置控件的StyleNumericUpDown这只发生在 Android 上,iOS 的所有选项卡看起来都很好。我不确定是什么导致了这个问题。任何帮助表示赞赏。如果您需要更多信息或代码片段,请告诉我。

标签: xamlxamarinxamarin.formsxamarin.androidstyles

解决方案


刚刚发现使用SyncfusionTheme导致这种行为。一旦我将它从 中删除MergedDictionaries,它就起作用了。


推荐阅读