【发布时间】:2021-01-16 14:21:57
【问题描述】:
我最近按照本教程在我的 Xamarin 应用中实现了暗模式支持:https://devblogs.microsoft.com/xamarin/modernizing-ios-apps-dark-mode-xamarin/
我已经使用 App.xaml 中定义的样式设置了标签颜色,如下所示:
<Style x:Key="label" TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource label}"/>
</Style>
<Style x:Key="labelLight" TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource labelLight}"/>
</Style>
颜色 label 和 labelLight 在名为 LightTheme.xaml 和 DarkTheme.xaml 的 ResourceDictionary 中定义。
我还为 iOS 实现了一个自定义渲染器,如下所示:
public class ThemeRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
return;
SetTheme();
}
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
if (TraitCollection.UserInterfaceStyle != previousTraitCollection.UserInterfaceStyle)
SetTheme();
}
private void SetTheme()
{
if (TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
{
App.Current.Resources = new DarkTheme();
App.AppTheme = Theme.Dark;
}
else
{
App.Current.Resources = new LightTheme();
App.AppTheme = Theme.Light;
}
}
}
这是我遇到麻烦的地方,我的 Android 版本的应用程序无法识别这些颜色(一切都在 LightGray 中)并且我的应用程序在 iOS 12 及更低版本下不再运行。我不知道该怎么做才能解决这个问题。
【问题讨论】:
-
在android中,你更新你的基本主题使用
Theme.AppCompat.DayNight了吗?在ios中,它似乎支持ios 13之后的暗模式。 -
我没有更新我的基本主题,你能帮我做吗?
-
我需要在 info.plist 中将 OS 目标设置为 13.0 吗?
-
现在可以用了吗?
标签: xaml xamarin.forms colors