IsolatedStorageSettings访问引发类型'System.IO.FileNotFoundException'的第一次机会异常

时间:2012-09-18 14:34:14

标签: c# windows-phone-7 exception isolatedstorage .net

我正在创建一个设置视图和一个“相关”设置类,如下所述:

How to: Create a Settings Page for Windows Phone

这不是我的第一次尝试,但是当我尝试从IsolatedStorageSettings获取ApplicationSettings时,我得到了mscorlib.dll 中类型为'System.IO.FileNotFoundException'的第一次机会异常。这发生在后面和相关视图模型中的视图代码中,在我的应用程序的其他部分(即其他视图的代码隐藏或视图模型)中它可以正常工作。为什么???

以下是代码:

AppSettings.cs

using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Windows;

namespace MetanoInLombardia
{
    public class AppSettings
    {
        IsolatedStorageSettings settings;

        const string IsAutoUpdateOn_KeyName = "AutoUpdateOn"; 

        const bool IsAutoUpdateOn_Default = true;

        public AppSettings()
        {
            try
            {
                if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
                {
                    settings = IsolatedStorageSettings.ApplicationSettings;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception while using IsolatedStorageSettings in AppSettings(): " + e.ToString());
            }
        }

        public void AddOrUpdateValue(string Key, Object value)
        {
            if (settings.Contains(Key))
            {
                if (settings[Key] != value)
                {
                    settings[Key] = value;
                    Save();
                }
            }
            else
            {
                settings.Add(Key, value);
                Save();
            }
        }

        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            if (!settings.Contains(Key))
            {
                AddOrUpdateValue(Key, defaultValue);
            }
            return (T)settings[Key];
        }

        public void Save()
        {
            settings.Save();
        }

        public bool IsAutoUpdateOn
        {
            get
            {
                return GetValueOrDefault<bool>(IsAutoUpdateOn_KeyName, IsAutoUpdateOn_Default);
            }
            set
            {
                AddOrUpdateValue(IsAutoUpdateOn_KeyName, value);
            }
        }
    }
}

SettingsViewModel.cs [extract]

using GalaSoft.MvvmLight;
using MetanoInLombardia.Model;
using System;
using System.Windows;
using System.IO.IsolatedStorage;

namespace MetanoInLombardia.ViewModel
{
    public class SettingsViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;
        private AppSettings settings = null;

        public SettingsViewModel(IDataService dataService)
        {
            _dataService = dataService;

            _dataService.GetApplicationTitle(
                (item, error) =>
                {
                    if (error != null)
                    {
                        return;
                    }
                    ApplicationTitle = item.Title;
                });

            this.settings = new AppSettings();
        }
...
    }
}

我在AppSettings.cs中的行

中出现异常
settings = IsolatedStorageSettings.ApplicationSettings;

0 个答案:

没有答案