【发布时间】:2020-12-20 05:18:31
【问题描述】:
我在 Xamarin 应用程序中遇到了一些奇怪的行为。 在共享项目中,我将所有基于控件的样式分隔在单个 xaml 文件中。
所有样式都组合在DefaultTheme.xaml 文件中。
然后,LightTheme.xaml 和 DarkTheme.xaml 将使用 DefaultTheme.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>
另外,NumericUpDown 控件的Style 未在第二个选项卡上设置。这只发生在 Android 上,iOS 的所有选项卡看起来都很好。
我不确定是什么导致了这个问题。任何帮助表示赞赏。
如果您需要更多信息或代码 sn-ps,请告诉我。
【问题讨论】:
标签: xaml xamarin xamarin.forms xamarin.android styles