bovender framework
C# framework that implements MVVM and more
UserSettingsViewModel.cs
1 /* UserSettingsViewModel.cs
2  * part of Bovender framework
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using Bovender.Mvvm;
24 using Bovender.Mvvm.Messaging;
25 
26 namespace Bovender.UserSettings
27 {
32  {
33  #region Public properties
34 
35  public UserSettingsBase UserSettings { get; set; }
36 
37  #endregion
38 
39  #region Commands
40 
41  public DelegatingCommand SaveCommand
42  {
43  get
44  {
45  if (_saveCommand == null)
46  {
47  _saveCommand = new DelegatingCommand(param => DoSave());
48  }
49  return _saveCommand;
50  }
51  }
52 
53  #endregion
54 
55  #region MVVM messages
56 
57  public Message<ViewModelMessageContent> SavedMessage
58  {
59  get
60  {
61  if (_savedMessage == null)
62  {
63  _savedMessage = new Message<ViewModelMessageContent>();
64  }
65  return _savedMessage;
66  }
67  }
68 
69  #endregion
70 
71  #region Implementation of ViewModelBase
72 
73  public override object RevealModelObject()
74  {
75  return UserSettings;
76  }
77 
78  #endregion
79 
80  #region Constructors
81 
82  public UserSettingsViewModel()
83  : base()
84  { }
85 
86  public UserSettingsViewModel(UserSettingsBase userSettings)
87  : this()
88  {
89  UserSettings = userSettings;
90  }
91 
92  #endregion
93 
94  #region Protected methods
95 
96  protected virtual void DoSave()
97  {
98  if (UserSettings != null)
99  {
100  UserSettings.Save();
101  SavedMessage.Send(new ViewModelMessageContent(this));
102  }
103  }
104 
105  #endregion
106 
107  #region Private fields
108 
109  private DelegatingCommand _saveCommand;
110  private Message<ViewModelMessageContent> _savedMessage;
111 
112  #endregion
113  }
114 }
void Save()
Saves the user settings to a file.
Message content that holds a reference to a view model.
Command that implements ICommand and accepts delegates that contain the command implementation.
General view model for UserSettings objects.
Base class for persistent settings; a replacement for the UserSettings.UserSettingsBase system which ...
override object RevealModelObject()
Returns the model object that this view model wraps or null if there is no wrapped model object...