mahapps metro showMessageAsync error: " Object reference not set to an instance of an object"

StackOverflow https://stackoverflow.com/questions/22148988

  •  19-10-2022
  •  | 
  •  

Question

I have this method

    public async void setState(int state)
    {
        switch (state)
        {
            default:
                await this.ShowMessageAsync("Title", "Message");
                break;
        }
    }

which I call from the cuntructor of the MetroWindow. But I get this error:

An exception of type 'System.NullReferenceException' occurred in MahApps.Metro.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

On the line

await this.ShowMessageAsync("Title", "Message");

How do I fix this?

Was it helpful?

Solution

ShowMessageAsync(...) uses the overlayBox that is not constructed yet so you will need to move the call outside of the constructor. https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Controls/MetroWindow.cs#L468

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top