获取静态上下文中ApplicationData.Current.LocalSettings的值

本文关键字:Current LocalSettings 的值 ApplicationData 静态 上下文 获取 | 更新日期: 2023-09-27 18:11:43

我有下一个代码:

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); }
            }
        }
    }

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

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

它抛出异常:

AppUser。ExistAuthData'抛出了类型的异常的系统。TypeInitializationException"

但是当我试图在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 projects'Events'EventsNotifier'EventsNotifier'Helpers'Settings.cs:line 9
   at EventsNotifier.Helpers.Settings.Authentification.get_SessionToken() in e:'New projects'Events'EventsNotifier'EventsNotifier'Helpers'Settings.cs:line 70
   at EventsNotifier.Helpers.AppUser.RestorePreviewUserState() in e:'New projects'Events'EventsNotifier'EventsNotifier'Helpers'AppUser.cs:line 13
   at EventsNotifier.Helpers.AppUser..cctor() in e:'New projects'Events'EventsNotifier'EventsNotifier'Helpers'AppUser.cs:line 8

获取静态上下文中ApplicationData.Current.LocalSettings的值

我试着再现你的问题,我只在调试时成功再现了它:

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

原因似乎是,如果你试图初始化LocalSettings(第一次调用),而主线程被停止,它抛出一个异常(你可以看到,甚至在调试器工具提示)。一旦发生这种情况,就没有办法再使用AppUser类了,因为异常是从它的静态构造函数抛出的,该构造函数只调用一次,并且在将来任何尝试访问它时都会重新抛出相同的异常。我几年前就写过一篇关于这种行为的博文。

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