AvaloniaUI / avalonia-docs

https://docs.avaloniaui.net/docs/welcome
64 stars 213 forks source link

Update opening-a-dialog.md #547

Open nojuslau opened 2 months ago

nojuslau commented 2 months ago

System.NullReferenceException: Object reference not set to an instance of an object." 10:09:51.696 [Error] 18512 <= " at Avalonia.MusicStore.Views.MainWindow.<.ctor>b__0_0(Action1 action) in Avalonia.MusicStore\Views\MainWindow.axaml.cs:line 14"

Either ensure that ViewModel is not nullable or add data context in constructor: DataContext = new MainWindowViewModel();

thevortexcloud commented 3 weeks ago

The view model is nullable. The code that is there just assumes it never will be, which it wont 99% of the time. The only time it should be null is if you for some reason create the dialogue without giving it a view model.

In fact: adding this check may actually create additional bugs since it will silently fail to register interaction handlers when the view is activated.