If you are getting the “Dialogs must be user-initiated.” error in Visual Studio, you are probably calling an OpenFileDialog or SaveFileDialog from the wrong place.

The most common ‘wrong place’ seems to actually be the debugger. If are stepping through your code and try to step over a dialog.ShowDialog() call, you will most definitely get this error. So before you go rewriting your code, try placing the breakpoint after the call, and see if that fixes your problem. Other causes include calling it from a callback on a different thread, in which case you might want to try using the Dispatcher.

For good MVVM pattern-type stuff, you might want to set up a service that will call dialogs for you. In fact, you may wish to do this anyway depending on the nature of your ViewModels.

Hope this helps someone, it sure helped me once I figured it out.

Happy Coding –csmacnz

UPDATE: There also seems to be a time or space limitation on how long after a user event you can actually call a Dialog, although in my case that time probably shouldn’t be being used up on the UI thread anyway. I guess I need to rethink my usage. Good thing it’s just a quick mock-up of code anyway.