C
C#•8mo 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•8mo 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
KillogeeOP•8mo 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..
Want results from more Discord servers?
Add your server