gpt4 book ai didi

.net - 如何将我的 View 模型属性绑定(bind)到应用程序设置?

转载 作者:行者123 更新时间:2023-12-03 10:51:53 25 4
gpt4 key购买 nike

我的 View 模型 MyViewModel 有一个属性 MyProperty,我需要从应用程序的设置读取/保存它。我的项目有 Settings.settings 文件,并且 Settings 类型有 MyProperty 属性。我需要将 MyViewModel.MyProperty 绑定(bind)到 Settings.MyProperty,以便对 MyViewModel.MyProperty 的任何更改都反射(reflect)在 Settings.MyProperty 中,反之亦然。我怎样才能做到这一点?

请注意,我无法从 Settings 派生 MyViewModel,因为 MyViewModel 已经派生了另一种类型。

编辑:我当然可以手动完成:在我的属性定义中读取和写入设置,但我问是否有更优雅的方法。

class MyViewModel : ViewModelBase
{
public int MyProperty { ... }
public MyViewModel()
{
// here i need to bind Settings.Default.MyProperty to this.MyProperty
}
}

最佳答案

您可以将 Settings 对象视为另一个 ViewModel(让它实现 INotifyPropertyChanged),而不是在 MyViewModel 上使用 MyProperty。您可以拥有多个 ViewModel。这显然会减少重复。

关于.net - 如何将我的 View 模型属性绑定(bind)到应用程序设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3980572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com