Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保存对存储在Properties.Settings中的ObservableCollection的更改_C#_Wpf - Fatal编程技术网

C# 如何保存对存储在Properties.Settings中的ObservableCollection的更改

C# 如何保存对存储在Properties.Settings中的ObservableCollection的更改,c#,wpf,C#,Wpf,在玩一个简单的世界时钟程序时,我遇到了一个问题:如何在程序运行之间持久化时钟对象的自定义可观察集合。我可以成功保存其他设置类型,如字符串、双精度和布尔值,但程序会话之间不会保存ObservableCollection 不会引发任何错误,调试器值似乎可以正常更新(Properties.Settings.Default.SavedClockscount增加),并且时钟添加到显示的列表中不会出现任何问题 这使我了解了下面代码的当前结构 设置.Designer.cs-手动创建 主窗口已初始化 n

在玩一个简单的世界时钟程序时,我遇到了一个问题:如何在程序运行之间持久化
时钟
对象的自定义
可观察集合
。我可以成功保存其他设置类型,如字符串、双精度和布尔值,但程序会话之间不会保存
ObservableCollection

不会引发任何错误,调试器值似乎可以正常更新(
Properties.Settings.Default.SavedClocks
count增加),并且时钟添加到显示的列表中不会出现任何问题

这使我了解了下面代码的当前结构



设置.Designer.cs-手动创建


主窗口已初始化

namespace WorldClock
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            if (Properties.Settings.Default.SavedClocks == null)
            {
                Properties.Settings.Default.SavedClocks = new ObservableCollection<Clock>
                {
                    //When list is empty, default with local clock
                    new Clock(System.TimeZoneInfo.Local, System.TimeZoneInfo.Local.StandardName)
                };

                Properties.Settings.Default.Save();
            }

            lvDataBinding.ItemsSource = Properties.Settings.Default.SavedClocks;
        }
    }
}

XAML-我不认为这其中任何一个有用,但这是我正在设置ItemSource的ListView


尝试将
设置Serializeas
属性添加到in Settings.Designer.cs的
SavedClocks
属性中,以指定集合应序列化为二进制数据的顺序:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
[global::System.Configuration.DefaultSettingValueAttribute(null)]
public ObservableCollection<Clock> SavedClocks
{
    get
    {
        return ((ObservableCollection<Clock>)(this["SavedClocks"]));
    }
    set
    {
        this["SavedClocks"] = value;
    }
}
[global::System.Configuration.UserScopedSettingAttribute()]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[全局::System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
[全局::System.Configuration.DefaultSettingValueAttribute(null)]
公共可观察收集保存的锁
{
得到
{
返回((ObservableCollection)(此[“SavedClocks”]);
}
设置
{
此[“SavedClocks”]=值;
}
}

尝试将
设置Serializeas
属性添加到in Settings.Designer.cs的
SavedClocks
属性中,以指定集合应序列化为二进制数据的顺序:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
[global::System.Configuration.DefaultSettingValueAttribute(null)]
public ObservableCollection<Clock> SavedClocks
{
    get
    {
        return ((ObservableCollection<Clock>)(this["SavedClocks"]));
    }
    set
    {
        this["SavedClocks"] = value;
    }
}
[global::System.Configuration.UserScopedSettingAttribute()]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[全局::System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
[全局::System.Configuration.DefaultSettingValueAttribute(null)]
公共可观察收集保存的锁
{
得到
{
返回((ObservableCollection)(此[“SavedClocks”]);
}
设置
{
此[“SavedClocks”]=值;
}
}

对于存储数据,收集不需要是可见的。在应用程序启动期间,您可以轻松地从存储的列表或数组初始化ObservableCollection。另请参阅这篇关于在应用程序设置中存储自定义类型的帖子:@Clemens-您链接的帖子是一个很好的来源,我一直在关注它,但不幸的是,它并不能解决我的问题,因为在运行之间数据不持久。尝试另一种类型是一种选择,但我想知道为什么我的方法不能按预期工作。THKSY您的时钟类可能应该有一个无参数构造函数。@Clemens-实际上,我已经有了,但在发布时意外地去掉了它。现在发布。谢谢你的邀请thought@PortlandRunner尝试从时钟类中删除INPC。为了存储数据,集合不需要是可观察的。在应用程序启动期间,您可以轻松地从存储的列表或数组初始化ObservableCollection。另请参阅这篇关于在应用程序设置中存储自定义类型的帖子:@Clemens-您链接的帖子是一个很好的来源,我一直在关注它,但不幸的是,它并不能解决我的问题,因为在运行之间数据不持久。尝试另一种类型是一种选择,但我想知道为什么我的方法不能按预期工作。THKSY您的时钟类可能应该有一个无参数构造函数。@Clemens-实际上,我已经有了,但在发布时意外地去掉了它。现在发布。谢谢你的邀请thought@PortlandRunner试着把INPC从时钟课上移除。这让我度过了难关,谢谢!对于下面的其他人,我还需要将
[字段:非序列化]
添加到
公共事件属性changedEventHandler属性changed[字段:非序列化]
添加到
公共事件属性changedEventHandler属性changedprivate void btn_AddRegion_Click(object sender, RoutedEventArgs e)
{
    TimeZoneInfo tz = timeZoneCombo.SelectedItem as TimeZoneInfo;

    Properties.Settings.Default.SavedClocks.Add(new Clock(tz, tbx_RegionName.Text));
    Properties.Settings.Default.Save();
}
<ListView  Name="lvDataBinding">
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
[global::System.Configuration.DefaultSettingValueAttribute(null)]
public ObservableCollection<Clock> SavedClocks
{
    get
    {
        return ((ObservableCollection<Clock>)(this["SavedClocks"]));
    }
    set
    {
        this["SavedClocks"] = value;
    }
}