【问题标题】:Xamarin Forms IOS 13 UIUserInterfaceStyle -Dark ModeXamarin Forms IOS 13 UIUserInterfaceStyle - 深色模式
【发布时间】:2023-03-24 15:14:01
【问题描述】:

我怎样才能使我的应用程序响应这个“新功能”。我现在想要一个唯一的灯光模式。 我看到了这个

UIUserInterfaceStyle style = UIUserInterfaceStyle.Light;

如何在我的 Xamarin Forms 应用上激活它

【问题讨论】:

    标签: xamarin.forms xamarin.ios ios13 ios-darkmode


    【解决方案1】:

    您可以通过创建两个资源来响应用户界面样式的变化:LightTheme 和 DarkTheme(它们将是 ResourceDictionaries)。在它们内部,您可以添加自定义颜色。例如:

    LightTheme.xaml:

    <ResourceDictionary
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="DarkMode.Styles.LightTheme">
    
        <Color x:Key="background">#FFFFFF</Color>
        <Color x:Key="mainLabel">#000000</Color>
    
    </ResourceDictionary>
    

    DarkTheme.xaml:

    <ResourceDictionary
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="DarkMode.Styles.DarkTheme">
    
        <Color x:Key="background">#000000</Color>
        <Color x:Key="mainLabel">#FFFFFF</Color>
    
    </ResourceDictionary>
    

    然后,您必须为 ContentPage 元素创建一个 CustomRenderer(当然,仅适用于 iOS):

    [assembly: ExportRenderer(typeof(ContentPage), typeof([YOUR_NAMESPACE].iOS.Renderers.PageRenderer))]
    namespace [YOUR_NAMESPACE].iOS.Renderers
    {
      public class PageRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
      {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
          base.OnElementChanged(e);
    
          if (e.OldElement != null || Element == null)
              return;
    
          try
          {
              SetTheme();
          }
          catch(Exception ex)
          {
    
          }
        }
    
        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();
            else
                App.Current.Resources = new LightTheme();
        }
      }
    }
    

    More information.

    【讨论】:

    • 我不确定这是否必要。某些页面似乎会自动更改背景/文本颜色。我不明白为什么有些页面会自动执行,而其他页面却没有。
    【解决方案2】:

    您可以按照前面的评论对模式更改做出反应。如果你想在你的应用程序中设置模式,你可以通过在你的AppDelegate.cs 中设置Window.OverrideUserInterfaceStyle 来实现,

    Window.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;

    这是关于如何在 Xamarin.Forms 中支持暗模式的完整博客文章, https://intelliabb.com/2019/11/02/how-to-support-dark-mode-in-xamarin-forms/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2020-01-30
      • 1970-01-01
      • 2020-02-18
      • 2020-04-13
      相关资源
      最近更新 更多