I have the following code and am still experiencing a "Dialogs must be user-initiated" exception on the ofd.ShowDialog();
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = ALLOWED_FILE_TYPES;
ofd.FilterIndex = 1;
ofd.Multiselect = false;
bool? userClickedOK = ofd.ShowDialog();
if (userClickedOK == true)
{.....}
}
From MSDN:
In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur.
I can't see how the few lines after the click event is taking up this time limit.
Any suggestions on how to avoid this?
Thanks
I found the cause of this.
The Loaded event is being subscribed to in the constructor, and while the constructor is called just once for the control, the loaded even is being called twice and hence the btnOpen_Click
event is being subscribed to twice.
I can fix it by unsubscribing from the loaded event in the control unloaded event but I'm still not sure why the Loaded is being called twice.