C
C#β€’3mo ago
Killogee

Non-Nullable field (WPF)

Im currently using MVVM and trying to get the viewmodel to work namespace WPFLearning.MVVM.ViewModel { class MainViewModel : ObservableObject { public HomeViewModel HomeVM { get; set; } private object _currentView; public object CurrentView { get { return _currentView; } set { _currentView = value; OnPropertyChanged(); } } public MainViewModel() { HomeVM = new HomeViewModel(); CurrentView = HomeVM; } } } But there is line under MainViewModel sayting that "Non-nullable field '_currentView' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. "
No description
2 Replies
Angius
Angiusβ€’3mo ago
object πŸ’€ And, yeah, the field can end up having no value So either give it a value in the constructor, or give it the value of null! to suppress the error Maybe making the property required could also solve it
Killogee
Killogeeβ€’3mo ago
soooo in my main window i had
<Window.DataContext>
<viewModel:HomeViewModel />
</Window.DataContext>
<Window.DataContext>
<viewModel:HomeViewModel />
</Window.DataContext>
and i needed it to say MainViewModel not home..