【问题标题】:Getting values of ApplicationData.Current.LocalSettings in static context在静态上下文中获取 ApplicationData.Current.LocalSettings 的值
【发布时间】:2013-05-02 12:05:58
【问题描述】:

我有下一个代码:

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' 引发类型异常 'System.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

【问题讨论】:

    标签: c# static windows-runtime microsoft-metro application-data


    【解决方案1】:

    我试着重现你的问题,我只设法重现它在调试吧: P>

    • 即使AppUser.ExistAuthDataApp构造函数第一个呼叫,它工作得很好,只要我没有把任何断点呼叫前。
    • 如果我把断点的调用之前和上空盘旋AppUser.ExistAuthData财产,我设法复制您的错误完全相同的堆栈跟踪。

    原因似乎是,如果你尝试初始化LocalSettings(第一次调用),而主线程停止时,它抛出一个异常(你可以看到,即使在调试器提示)。一旦发生这种情况,有没有办法使用AppUser类更多,因为异常是从静态构造函数被称为只有一个时间,在将来任何企图ACCES它重新抛出同样的异常抛出。我已经blogged about this behavior年前。 P>

    如果您访问LocalSettings这一呼吁之前,你已经初始化它,并确保将来试图访问即使主线程停止它不会失败。这样一来,一切工作正常,即使你将鼠标悬停在调试器属性。 P>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2012-04-29
      • 2011-05-22
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多