【问题标题】:Xamarin forms - Style in androidXamarin 表单 - android 中的样式
【发布时间】: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> 

颜色 labellabelLight 在名为 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


【解决方案1】:

例如在android活动中

 [Activity(Label = "@string/app_name", Theme = "@style/MyTheme", MainLauncher =true)]
 public class MainActivity : AppCompatActivity{

   ...
 }

然后在您的 Resources/values/styles.xml 中:

<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> // base theme to use Theme.AppCompat.DayNight
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="android:colorPrimary">#ff29b6f6</item> 
</style>

你可以阅读this

【讨论】:

  • 看来你链接的文章正是我想要的,非常感谢。
猜你喜欢
  • 2020-04-18
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多