MattNorman
Well-known member
- Joined
- May 22, 2021
- Messages
- 98
- Programming Experience
- 1-3
I have made the decision to move to WPF and stop trying to make windows forms do what I want because it's all I know.
I have watched several videos on the MVVM model and how to set this up however they all seems to do things a little different and I have gotten quite confused along the way.
In my current example I have the following:
1. In App.xaml code behind I first check if this is the first time the app has been launched and if settings needs to be configured
2. I then create an instance of a window (AutoSizeWindow) that I have and set its content to a new instance of my SetupTypeView.
3. Once the user clicks the continue button, it then creates a new instance of my SystemSettingsView and sets that to the windows content.
3. In my view's code behind I have a variable called 'systemSettings' and after the Initialize in the constructor, I set the variable to a new instance of my 'SystemSettingsViewModel' and then set the views DataContext to the viewmodel instance
4. The system settings view model checks some provided parameters in the constructor and based on these will either leave it's instance of my SystemSettingsDataModel with default values, or load existing settings.
5. When it is doing the loading of settings and calling the OnPropertyChanged event, I get a null reference exception. It's unclear what is set to null however as both the passed string is not null.
I may be doing something completely wrong here but it's difficult to follow with so many different implementations in different videos.
I would appreciate any assistance.
App.xaml.cs
SetupTypeView.xaml.cs
SystemSettingsView - Constructor
SystemSettingsViewModel
ObservableObject
I have watched several videos on the MVVM model and how to set this up however they all seems to do things a little different and I have gotten quite confused along the way.
In my current example I have the following:
1. In App.xaml code behind I first check if this is the first time the app has been launched and if settings needs to be configured
2. I then create an instance of a window (AutoSizeWindow) that I have and set its content to a new instance of my SetupTypeView.
3. Once the user clicks the continue button, it then creates a new instance of my SystemSettingsView and sets that to the windows content.
3. In my view's code behind I have a variable called 'systemSettings' and after the Initialize in the constructor, I set the variable to a new instance of my 'SystemSettingsViewModel' and then set the views DataContext to the viewmodel instance
4. The system settings view model checks some provided parameters in the constructor and based on these will either leave it's instance of my SystemSettingsDataModel with default values, or load existing settings.
5. When it is doing the loading of settings and calling the OnPropertyChanged event, I get a null reference exception. It's unclear what is set to null however as both the passed string is not null.
I may be doing something completely wrong here but it's difficult to follow with so many different implementations in different videos.
I would appreciate any assistance.
App.xaml.cs
C#:
private void AppStart(object sender, StartupEventArgs e) { //Set default theme colors. ThemeManager.ChangeAccentColor(Colors.DarkRed); //Check if first time setup. If so, run through setup process and then go back to start to re-process user info. if (RegistryManager.GetRegistrySetting("FirstTimeSetup") == "true" || RegistryManager.GetRegistrySetting("FirstTimeSetup") == string.Empty) { //Display setup type view. AutoSizeWindow wdw = new AutoSizeWindow(); wdw.cntMain.Content = new SetupTypeView(); wdw.ShowDialog(); } else { //Validate user etc //Load main window and view } }"]private void AppStart(object sender, StartupEventArgs e)
{
//Set default theme colors.
ThemeManager.ChangeAccentColor(Colors.DarkRed);
//Check if first time setup. If so, run through setup process and then go back to start to re-process user info.
if (RegistryManager.GetRegistrySetting("FirstTimeSetup") == "true" || RegistryManager.GetRegistrySetting("FirstTimeSetup") == string.Empty)
{
//Display setup type view.
AutoSizeWindow wdw = new AutoSizeWindow();
wdw.cntMain.Content = new SetupTypeView();
wdw.ShowDialog();
}
else
{
//Validate user etc
//Load main window and view
}
}
SetupTypeView.xaml.cs
C#:
private void btnContinue_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(cbSetupType.Text))
{
if (cbSetupType.Text == "Server")
{
AutoSizeWindow win = new AutoSizeWindow();
SystemSettingsView view = new SystemSettingsView(SettingsFormType.Server, FormMode.Setup);
SystemSettingsViewModel model = new SystemSettingsViewModel(FormMode.Setup, SettingsFormType.Server);
view.DataContext = model;
win.cntMain.Content = view;
win.Show();
Window parentWin = Window.GetWindow(this);
parentWin.Close();
}
else if (cbSetupType.Text == "User")
{
AutoSizeWindow win = new AutoSizeWindow();
SystemSettingsView view = new SystemSettingsView(SettingsFormType.User, FormMode.Setup);
win.cntMain.Content = view;
win.Show();
Window parentWin = Window.GetWindow(this);
parentWin.Close();
}
}
else
{
CustomMessageBox mb = new CustomMessageBox("System Setup", "Please choose a setup type before continuing");
mb.ShowDialog();
}
}
SystemSettingsView - Constructor
C#:
public SystemSettingsView(SettingsFormType FormType, FormMode FormMode)
{
InitializeComponent();
vm = new SystemSettingsViewModel(FormMode, FormType);
this.DataContext = vm;
}
SystemSettingsViewModel
C#:
public class SystemSettingsViewModel : ObservableObject
{
#region Properties
private SystemSettingsModel systemSettings = new SystemSettingsModel();
public SystemSettingsModel SystemSettings
{
get { return systemSettings; }
set { systemSettings = value; OnPropertyChanged("systemSettings"); }
}
private FormMode formMode;
private SettingsFormType formType;
#endregion
#region Constructor
public SystemSettingsViewModel(FormMode FormMode, SettingsFormType FormType)
{
formMode = FormMode;
formType = FormType;
if (formMode == FormMode.Setup) { SystemSettings = DataManagerSystemSettings.GetSystemSettings(); }
}
#endregion
}
ObservableObject
C#:
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Last edited by a moderator: