- I am writing a small scientific program. It has lots of variables as input.
- The user has to enter all the input variables in the window (or user interface). But, I cannot fit all the input in a single window (XAML). Thus, I have created several Views where the user just press the NEXT button to enter data in the next View.
- All of these Views have associated ViewModels. They all inherit from a base ViewModel.
- So, my question is: Do I write the properties of all the variables inside the base ViewModel? Like this:
namespace ScienceProgram
{
public abstract class BaseViewModel : INotifyPropertyChanged
{
#region Usual Boiler-plate stuff for BindableBase
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion
#region Properties (All scientific parameters and calculations )
public double ParamA { get; set; }
public double ParamB { get; set; }
// ...
// Lots of parameters //
// ...
public double ParamA23 { get; set; }
public double TotalLength()
{
return ParamA + ParamB + ParamA23;
}
// ...
// Lots of other methods
// ...
#endregion
}
}
- Or do I create a separate class (e.g. ScienceParameters.cs) for all input parameters and do as follows (this is what I am doing):
namespace ScienceProgram
{
public abstract class BaseViewModel : INotifyPropertyChanged
{
// Global Parameter // Shared across ViewModels //
public static ScienceParameters scienceParameters = new ScienceParameters ();
#region Usual Boiler-plate stuff for BindableBase
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion
}
}
- I am using the latter approach. When the user enters the data in the textbox, I simply store it as:
namespace ScienceProgram
{
public class UserInputView1ViewModel : BaseViewModel
{
#region User Input
private double _paramA;
public double ParamA
{
get { return _paramA; }
set
{
_paramA = value;
OnPropertyChanged("ParamA");
// Store Value //
scienceParameters.ParamA = value;
}
}
}
}
#endregion
- Which is the the right way? Or is there a better way?
- I have been looking for some best practices, but I keep getting directed to EventAggregator and using a singleton. I don't think I need those.
- I think the solution is "to pass a model into constructor of a view model.", as many have said. But I am a bit confused on how to do it. Wouldn't that create a new instance in each viewModel?
- Sorry, if this sounds like a silly question, but I have been looking for a straight answer for the past week, and have not yet figured out the solution.
Many thanks in advance.