MahApps Context is not registered on startup

Viewed 156

I have WPF MVVM app with the MahApps Shell Window which loads couple of Pages. On the ShellWindow I have setup the DialogParticipation.Register which I inject to Page ViewModels. So far this approach works when the code executes via commands. However, in one of the Pages I have to load data from external API in the constructor. When request errors I am showing the mahapps await _dialogCoordinator.ShowMessageAsync(this, "Error", ex.Message, MessageDialogStyle.Affirmative); This is where the well known error appears Context is not registered. Consider using DialogParticipation.Register in XAML . it feels like it is a timing issue within constructor initialiazation but I am not sure how to deal with it properly.

<mah:MetroWindow x:Class="ShellWindow"
                bla bla
                 xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                 Dialog:DialogParticipation.Register="{Binding}"
                 mc:Ignorable="d">

 public ShellWindow(bool isData)
        {
           
            Main.Content = new PageOne(DialogCoordinator.Instance, this);
        }

Page One

public PageOne(IDialogCoordinator dialogCoordinator, ShellWindow shellWindow)
{
    InitializeComponent();

    _dialogCoordinator = dialogCoordinator;
    _shellWindow = shellWindow;
    DataContext = this;
   
    new Action(async () => await LoadReports())();
}
    
private async Task LoadReports()
{
    try {
        // API Request
    }
    catch (Exception ex)
    {
       // This is where the error happens
         _ = await _dialogCoordinator.ShowMessageAsync(this, "Error", ex.Message, MessageDialogStyle.Affirmative);
    }
}
0 Answers
Related