使用Xamarin.Forms 4.3.0.947036在Android和iOS上实现正常模式和暗模式,而没有闪烁 [英] Normal and Dark Mode implementation on both Android and iOS without the flashes using Xamarin.Forms 4.3.0.947036

查看:120
本文介绍了使用Xamarin.Forms 4.3.0.947036在Android和iOS上实现正常模式和暗模式,而没有闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始帖子中进行了讨论之后,由于 iOS 发现了一个问题,我们需要为它的答案打开一个新问题.


问题:

尝试此操作并对结果进行故障排除后, iOS public static async Task<Theme> GetOperatingSystemThemeAsync() { Theme systemTheme = await DependencyService.Get<IEnvironment>().GetOperatingSystemTheme().ConfigureAwait(true); SetTheme(systemTheme); return systemTheme; }

以及在iOS上尝试使用它时出现的问题:

使用此非异步Environment_iOS代码位于App.cs第53行的SetTheme()我有一个

System.NullReferenceException Message =对象引用未设置为对象的实例."在"UIViewController rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;

如果您不在App()中调用它,而仅在App.OnStart()中调用它,则此 https://github.com/Jakar510/FlickerTest

解决方案

问题

UIApplication.SharedApplication.KeyWindow为空,因为Xamarin.Forms尚未生成它.

解决方案

将对UIApplication.SharedApplication.KeyWindow的调用从Xamarin.Forms.Application的构造函数移动到Xamarin.Forms.Application.OnStart().

我还向您发送了一个PR,该PR实现了此修复程序,还正确地为您的Light Theme和Dark Theme实现了MergedDictionaries:Original Post, we needed to open a new question for its answer due to an issue found with iOS.


The Problem:

After attempting this and troubleshooting the results, the iOS async code doesn't work correctly. While Android works ok, the code below in App() doesn't seem to work correctly for iOS, as I apparently can't have await-ables in the App() which is needed to get the coloring correct when navigating.

Theme theme = await DependencyService.Get<IEnvironment>().GetOperatingSystemTheme();

even when using _ = GetOperatingSystemTheme().ConfigureAwait(true); in the App() the code isn't called, and therefore the Theme isn't set:

public static async Task<Theme> GetOperatingSystemThemeAsync()
{
    Theme systemTheme = await DependencyService.Get<IEnvironment>().GetOperatingSystemTheme().ConfigureAwait(true);

    SetTheme(systemTheme);
    return systemTheme;

}

And the Issue when trying to use it for iOS:

Using this non-async Environment_iOS code and the SetTheme() at Line 53 of the App.cs I got a

System.NullReferenceException Message=Object reference not set to an instance of an object". at the line "UIViewController rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;

If you don't call it in App() and instead in only in App.OnStart(), this flashing issue occurs.

Troubleshooting Results:

Whatever the default is in your app, that will flash when navigation occurs, In other words, a default white background will flash before the page is rendered to dark mode. Setting the defaults (whatever colors for the background needed. i.e. App.Current.Resources = new DarkModeTheme() for dark pages) before the page is called (i.e. SomePage()) gives the correct results as it must be done before navigation occurs and the page is rendered.

Example Repository: https://github.com/Jakar510/FlickerTest

解决方案

Problem

UIApplication.SharedApplication.KeyWindow is null because Xamarin.Forms hasn't yet generated it.

Solution

Move the call to UIApplication.SharedApplication.KeyWindow from the constructor of Xamarin.Forms.Application to Xamarin.Forms.Application.OnStart().

I also sent you a PR which implements this fix and also correctly implements MergedDictionaries for your Light Theme and Dark Theme: https://github.com/Jakar510/FlickerTest/pull/1

这篇关于使用Xamarin.Forms 4.3.0.947036在Android和iOS上实现正常模式和暗模式,而没有闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆