From b98358a5dc4340aff2faf08b78de6d98cdf3ad4e Mon Sep 17 00:00:00 2001 From: nojuslau <30390151+nojuslau@users.noreply.github.com> Date: Sun, 22 Sep 2024 10:20:02 +0300 Subject: [PATCH] Update opening-a-dialog.md 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(); --- .../music-store-app/opening-a-dialog.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/tutorials/music-store-app/opening-a-dialog.md b/docs/tutorials/music-store-app/opening-a-dialog.md index f6906f77b..527a767b7 100644 --- a/docs/tutorials/music-store-app/opening-a-dialog.md +++ b/docs/tutorials/music-store-app/opening-a-dialog.md @@ -145,8 +145,13 @@ private async Task DoShowDialogAsync(InteractionContext - action(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync))); +this.WhenActivated(action => +{ + if (viewModel != null) + { + action(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync))); + } +} ``` This means that whenever the main window view is activated, the `DoShowDialogAsync` handler is registered. The action is disposable, so that _ReactiveUI_ can clean up the registration when the main window view is not on the screen. @@ -167,7 +172,12 @@ namespace Avalonia.MusicStore.Views { InitializeComponent(); this.WhenActivated(action => - action(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync))); + { + if (viewModel != null) + { + action(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync))); + } + } } private async Task DoShowDialogAsync(InteractionContext