微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在等待期间静态类实例会为空?

如何解决为什么在等待期间静态类实例会为空?

我有一个在 Android 中运行的部分完成的 Xamarin 解决方案。我正在尝试添加 UWP 支持(我知道这还不是全部)。我在下面的代码调用 State.RestoreSystemIndex()。我在第一行有一个断点,我可以看到 CurrentSystem 已填充(如我所料)。我逐步执行代码,当 return await SecureStorage.GetAsync(key) 完成时,CurrentSystem 现在为 null,当然对它的引用失败。什么会使 CurrentSystemawait SecureStorage.GetAsync(key) 期间变为 null?

    public class Configuration
    {
        public const int SystemIndexUndefined = -1;

        public int SystemIndex = SystemIndexUndefined;
        /* etc. */
    };

    public static class State
    {
        public static Configuration CurrentSystem = new Configuration();
        /* snip */
        private static async Task<string> OurGetAsync(string key)
        {
            try
            {
                return await SecureStorage.GetAsync(key);
            }
            catch (Exception ex)
            {
                 Logger.LogMsg($"Exception {ex} thrown by GetAsync({key})");
            }
            return null;
        }

        public static async void RestoreSystemIndex()
        {
            string s = await OurGetAsync("SystemIndex");
            if (s == null) CurrentSystem.SystemIndex = Configuration.SystemIndexUndefined;    // Not recorded
            else CurrentSystem.SystemIndex = Convert.ToInt32(s);
        }
        /* etc. */

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。