0

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

如何:为 Windows Phone 创建设置页面

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

这是代码:

应用设置.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 [提取]

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;
4

0 回答 0