Search code examples
windows-8windows-store-appsunauthorizedaccessexceptimessagedialog

Call MessageDialog from Property Changed handler Store App


I'm trying to call a MessageDialog out of a PropertyChanged Handler. The first call is always successful, but when the Dialog gets called a second time, I get an UnauthorizedAccessException.

I've tried to wrap the call in a Dispatcher, but I got the same behavior.

Here's the code (snippet of MainPage.xaml.cs):

void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
  await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  {
    showMessage("Message", "Title");
  });
}

async void showMessage(String message, String title)
{
  MessageDialog dialog = new MessageDialog(message, title);
  await dialog.ShowAsync();
}

Could anybody please help me with this issue?


Solution

  • I think your problem is that multiple property changes will cause multiple calls to display the dialog. You should only ever display one dialog at a time:

    bool _isShown = false;
    async void showMessage(String message, String title)
    {
        if (_isShown == false)
        {
            _isShown = true;
    
            MessageDialog dialog = new MessageDialog(message, title);
            await dialog.ShowAsync();
    
            _isShown = false;
        }
    }