使用Xamarin.Forms 4.3.0.947036

Original Post上进行了一些讨论之后,由于 iOS 发现的问题,我们需要为其回答打开一个新问题。


问题:

尝试此操作并对结果进行故障排除后, iOS async code无法正常运行。虽然 Android 可以正常运行,但是App()中的以下代码似乎无法在 iOS 上正常运行,因为我显然无法等待-App()中的-导航时需要正确的颜色。

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

即使在_ = GetOperatingSystemTheme().ConfigureAwait(true);中使用App()也不调用代码,因此未设置主题:

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

    SetTheme(systemTheme);
    return systemTheme;

}

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

使用此non-async Environment_iOS codethe SetTheme() at Line 53 of the App.cs我得到了

  

System.NullReferenceException Message =对象引用未设置为对象的实例。”在“ UIViewController rootController = UIApplication.SharedApplication.keywindow.RootViewController;

如果您没有在App()中调用它,而仅在App.OnStart()中调用了此flashing issue occurs

故障排除结果:

无论应用程序中的默认设置是什么,它都会在导航时闪烁。换句话说,在页面呈现为暗模式之前,默认的白色背景会闪烁。在调用页面(即App.Current.Resources = new DarkModeTheme())之前设置默认值(所需背景的各种颜色,即SomePage()用于深色页面)会产生正确的结果,因为必须在导航和页面被执行之前完成此操作。呈现。

示例存储库:https://github.com/Jakar510/FlickerTest

hliyouheng 回答:使用Xamarin.Forms 4.3.0.947036

问题

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

解决方案

将调用从UIApplication.SharedApplication.KeyWindow的构造方法移动到Xamarin.Forms.Application

我还向您发送了一个PR,该PR实现了此修复程序,还正确地为您的Light Theme和Dark Theme实现了MergedDictionaries:https://github.com/Jakar510/FlickerTest/pull/1

本文链接:https://www.f2er.com/3148989.html

大家都在问