Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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# 是否使用ICommand将设置保存到App.Config?_C#_Wpf_Mvvm_Command - Fatal编程技术网

C# 是否使用ICommand将设置保存到App.Config?

C# 是否使用ICommand将设置保存到App.Config?,c#,wpf,mvvm,command,C#,Wpf,Mvvm,Command,我正在玩MVVM,了解模式中涉及的内容。我正在编写的第一个应用程序是一个非常小的应用程序,它基本上显示App.Config中的两个设置 我的目标是能够在单击按钮时写入此app.config 我的问题在于,我不知道如何连接一个命令来将这项工作委托给,或者这是否是一种方法 我的App.config非常简单: <configuration> <appSettings> <add key="duration" value="100" /> <

我正在玩MVVM,了解模式中涉及的内容。我正在编写的第一个应用程序是一个非常小的应用程序,它基本上显示App.Config中的两个设置

我的目标是能够在单击按钮时写入此app.config

我的问题在于,我不知道如何连接一个命令来将这项工作委托给,或者这是否是一种方法

我的App.config非常简单:

 <configuration>
  <appSettings>
    <add key="duration" value="100" />
    <add key="operators" value="10" />
  </appSettings>
</configuration>
所以,模型负责实际的数据访问,这就是我们想要的,对吗

此外,我还有一个ViewModel(ViewModelBase实现了INotifyPropertyChanged):

该视图是一个xaml usercontrol,实例化如下:

public partial class MainWindow : Window
{
    public SettingsViewModel SettingsViewModel { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        Settings settings = new Settings();

        SettingsViewModel = new SettingsViewModel(settings);
    }
}
最后还有一个SaveCommand,它实现了ICommand,现在基本上是空的。我已将命令连接到视图中的一个按钮

但基本上,现在怎么办?保存值的最佳方法是什么?
我正在研究的这个例子是不是太做作了

您只需在类中实现ICommand接口,并将SaveCommand属性设置为此类的实例。您可以使用一些第三方命令类,如prism库的DelegateCommand或RelayCommand。 有关ICommand的示例实现,请参阅以下网页;

然后,将要采取的行动记录在命令中

this.SaveCommand= new SaveCommand((o) =>
        {
            //change the model
        });

我建议使用非常有用的

基本上,您将公开一个
ICommand
公共属性,该属性返回一个
RelayCommand
实例:

private RelayCommand myCommand;

public ICommand MyCommand
{
    get
    {
        return this.myCommand;
    }
}

...

// in constructor of the view model:
this.myCommand = new RelayCommand(this.MyCommandImplementation);

...

private void MyCommandImplementation()
{
    // Save your parameters here from your model
}
代码中的奇怪之处在于,您实际上已经将设置保存在名为
Duration
的公共属性的setter中。 您可以改为(防止每次修改属性时保存)在ViewModel中使用私有变量:

private int duration;
public int Duration
{
    get { return this.duration; }
    set
    {
        if (this.duration != value)
        {
            this.duration = value;
            RaisePropertyChanged("Duration");
        }
    }
}
因此,当您修改绑定到
Duration
属性的UI字段时,您只会更新private
Duration
字段。因此,您只能将其保存到
MyCommandImplementation
方法中的app.config文件中

private void MyCommandImplementation()
{
    this.Settings.Duration = this.Duration;
}
还要注意的是,您的
设置管理有点复杂(您先删除设置,然后再添加设置,为什么?)。
最后一件事:在您的视图中,您当前正在将视图本身用作datacontext。您必须指定您的ViewModel:

this.SettingsViewModel = new SettingsViewModel(settings);
this.DataContext = this.SettingsViewModel;

此外,我认为实例化模型不是视图的作用。我将从ViewModel中实例化设置。

我在一个名为“SaveCommand”的类中,正如我对问题的描述所述。是的,由于保存行为,我想知道在这种情况下,命令是否是不必要的,因为我的示例太做作了。设置是这样做的,因为似乎没有编辑方法。@fuaaark好吧,你可以选择你真正想要的。如果您的目标是“在单击按钮时能够写入此app.config”,那么您需要一个按钮和一个
ICommand
。如果您不需要按钮,那么只需保存到属性setter中的app.config文件,就不需要
ICommand
。我将使用命令实现它,以了解更多内容:)但我可能应该考虑一些SaveSettings()方法,并从命令中调用它,以免使用数据访问逻辑来处理命令。是吗?@fuaaark在上面的代码中,您可以看到只有一个私有字段和一个公共属性公开了该命令。所有的逻辑都是通过
MyCommandImplementation
方法实现的。用这种方法编写所有保存逻辑是完全正确的。从
MyCommandImplementation()
调用
SaveSettings()
方法并没有真正的帮助,您可以直接在
MyCommandImplementation
中编写逻辑。
private void MyCommandImplementation()
{
    this.Settings.Duration = this.Duration;
}
this.SettingsViewModel = new SettingsViewModel(settings);
this.DataContext = this.SettingsViewModel;