vendredi 8 août 2014

c# - boîtes de dialogue doivent être exception lancée par l'utilisateur après que l'utilisateur clic - Stack Overflow


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




Ok, have not tested this myself but maybe I found a hint in a quite similar SO question/answer:


private OpenFileDialog OpenFileDialog {get;set;}

public Ctor()
{
OpenFileDialog = new OpenFileDialog();
}

private void btnOpen_Click(object sender, RoutedEventArgs e)
{
...
... OpenFileDialog.ShowDialog();
...
}

Instantiate the dialog in the constructor and only call the ShowDialog method inside your button click handler without instantiating a new dialog.


[Edit] That's the question/answer I mentioned.




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.



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



Ok, have not tested this myself but maybe I found a hint in a quite similar SO question/answer:


private OpenFileDialog OpenFileDialog {get;set;}

public Ctor()
{
OpenFileDialog = new OpenFileDialog();
}

private void btnOpen_Click(object sender, RoutedEventArgs e)
{
...
... OpenFileDialog.ShowDialog();
...
}

Instantiate the dialog in the constructor and only call the ShowDialog method inside your button click handler without instantiating a new dialog.


[Edit] That's the question/answer I mentioned.



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.


0 commentaires:

Enregistrer un commentaire