本文介绍了在静态上下文中获取ApplicationData.Current.LocalSetting的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个代码:

public static class AppUser
    {
        static AppUser()
        {
            RestorePreviewUserState();
        }

        private static void RestorePreviewUserState()
        {
            var storedToken = Settings.Authentification.SessionToken; //Here I gets my settings
            var storedUserId = Settings.Authentification.CurrentUserId; 

            if (storedToken == null || storedUserId == default(int)) return;
            AuthToken = storedToken;
            CurrentUserId = storedUserId;
        }

        public static bool ExistAuthData
        {
            get
            {
                return CurrentUserId != default(int) && AuthToken != null;
            }
        }

        private static string _authToken;
        public static string AuthToken
        {
            get { return _authToken; }
            set
            {
                _authToken = value;
                Settings.Authentification.SessionToken = _authToken;
                AuthHeader = new AuthHeader(_authToken);
            }
        }

        private static int _currentUserId;
        public static int CurrentUserId
        {
            get { return _currentUserId; }
            set
            {
                _currentUserId = value;
                Settings.Authentification.CurrentUserId = _currentUserId;
            }
        }
    }


    public class LocalSettings : ILocalSettings
    {
        public T GetValue<T>(string key)
        {
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
                return (T)ApplicationData.Current.LocalSettings.Values[key];

            return default(T);
        }

        public void SetValue(string key, object value)
        {
            if (value == null)
                ApplicationData.Current.LocalSettings.Values.Remove(key);

            ApplicationData.Current.LocalSettings.Values[key] = value;
        }
    }

    public interface ILocalSettings
    {
        T GetValue<T>(string key);
        void SetValue(string key, object value);
    }

    public static class Settings
    {
        private static readonly ILocalSettings _settings;

        static Settings()
        {
            _settings = new LocalSettings();
        }

        public static class Authentification
        {
            private const string CurrentUserKey = "CurrentUserId";
            public static int CurrentUserId
            {
                get { return _settings.GetValue<int>(CurrentUserKey); }
                set { _settings.SetValue(CurrentUserKey, value); }
            }

            private const string SessionTokenKey = "SessionToken";
            public static string SessionToken
            {
                get { return _settings.GetValue<string>(SessionTokenKey); }
                set { _settings.SetValue(SessionTokenKey, value); }
            }
        }
    }

当我的应用程序启动时,我尝试检查AppUser中是否存在ExistAuthData

if (!AppUser.ExistAuthData)
            {
                ...
            }

它抛出我异常:

但当我尝试在AppUser.ExistAuthData之前获取值时,一切都很好:

var temp = ApplicationData.Current.LocalSettings.Values.ContainsKey("Anykey");
if (!AppUser.ExistAuthData)

为什么会发生这种情况?

更新

 at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
   at Windows.Storage.ApplicationData.get_Current()
   at EventsNotifier.Helpers.LocalSettings.GetValue[T](String key) in e:New projectsEventsEventsNotifierEventsNotifierHelpersSettings.cs:line 9
   at EventsNotifier.Helpers.Settings.Authentification.get_SessionToken() in e:New projectsEventsEventsNotifierEventsNotifierHelpersSettings.cs:line 70
   at EventsNotifier.Helpers.AppUser.RestorePreviewUserState() in e:New projectsEventsEventsNotifierEventsNotifierHelpersAppUser.cs:line 13
   at EventsNotifier.Helpers.AppUser..cctor() in e:New projectsEventsEventsNotifierEventsNotifierHelpersAppUser.cs:line 8

推荐答案

我尝试重现您的问题,但只能在调试时重现:

  • 即使AppUser.ExistAuthDataApp构造函数中的第一个调用,只要我在调用之前没有放置任何断点,它就可以正常工作。
  • 如果我将断点放在调用之前并将鼠标悬停在AppUser.ExistAuthData属性上,我成功地用完全相同的堆栈跟踪重现了您的错误。

原因似乎是,如果在主线程停止时尝试初始化LocalSettings(第一个调用),它会抛出一个异常(您甚至可以在调试器工具提示中看到这一点)。一旦发生这种情况,就不能再使用AppUser类,因为异常是从它的静态构造函数引发的,该构造函数只被调用一次,并且在以后任何访问它的尝试时都会重新抛出相同的异常。我已经blogged about this behavior年了。

如果您在调用之前访问LocalSettings,则您已经对其进行了初始化,以确保即使主线程停止,以后访问它的尝试也不会失败。这样,即使您将鼠标悬停在调试器中的属性上,一切也能正常工作。

这篇关于在静态上下文中获取ApplicationData.Current.LocalSetting的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:42