2
votes

This is the error message.

An exception of type 'System.InvalidOperationException' occurred in System.Windows.ni.dll but was not handled in user code Additional information: Error displaying MessageBox. The most common reason is attempting to call Show while an application is launching or being activated. Wait for page navigation events before calling Show.

It happens right at the beginning when the app is launching and stopped at this function in the first if statement when trying to show the message box.

Code source = How to implement a trial experience in a Windows Phone app

    private void CheckLicense()
    {
        // this displays a dialog so that we can simulate trial mode being on or off. 
#if DEBUG
        string message = "Press 'OK' to simulate trial mode. Press 'Cancel' to run the application in normal mode.";
        if (MessageBox.Show(message, "Debug Trial",
             MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        {
            _isTrial = true;
        }
        else
        {
            _isTrial = false;
        }
#else
        _isTrial = _licenseInfo.IsTrial();
#endif
    }
2
where did you put this CheckLicense() function? - har07
its defined in the App class and is called in Application_Launching and Application_Activated, same as the MSDN example - erotavlas
i removed the if statement and just set the value manually, so its working, just not sure why the message box can't show, maybe there's some Debug setting that I need to allow the message box? - erotavlas
nope. As the error message suggest, you can't show message box until the moment your application UI ready (in Page_Load f.e). - har07
why are they calling it like that in the example if it doesn't work. I never tried their code solution, just copied it into my project. I guess I'll have to download any try it to compare. - erotavlas

2 Answers

3
votes

As the error indicates it is not possible to show the message box before the first page navigation.

MSDN has a sample specific for trial experience on Windows Phone 8. It's authored by the Windows Phone team, and it does not have any message boxes in the initialization code.

0
votes

I had the same issue. Here's the solution. You need to enclose your message box within [Deployment.Current.Dispatcher.BeginInvoke] For example:

Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        MessageBox.Show(message, title, MessageBoxButton.OK);
    });