Windows Phone 8.1应用程序在首次启动时访问本地设置时崩溃(UnauthorizedException)

时间:2014-08-24 09:27:31

标签: c# windows-phone-8 windows-runtime windows-phone-8.1

我的项目现在几乎准备好了。要检查应用程序是否是第一次启动,我在App.OnLaunched()函数中访问应用程序的本地文件夹,如果为null则设置新值。条件语句中的代码似乎正确执行,因为应用程序在后续运行中正常工作。任何人都可以帮助解决这个问题,看看该应用程序是否可以第一次运行而不会崩溃?

protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
    if (System.Diagnostics.Debugger.IsAttached)
    {
        this.DebugSettings.EnableFrameRateCounter = true;
    }
#endif


    Frame rootFrame = Window.Current.Content as Frame;

    var localSettings  = Windows.Storage.ApplicationData.Current.LocalSettings;

    if (localSettings.Values["waslaunched"]==null)
    {
        //Code for first launch
        localSettings.Values["waslaunched"] = "launched";
    }

    //Other autogenerated code in OnLaunched()
}

调试器显示以下异常 System.UnauthorizedException:拒绝访问

1 个答案:

答案 0 :(得分:0)

而不是检查null尝试下面的代码

 Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        if (!localSettings.Values.ContainsKey("waslaunched"))
        {
           localSettings.Values.Add("waslaunched", "launched");
        }

希望这有帮助!