launching an OpenFileDialog

Mar 14, 2011 at 5:01 PM


I am trying to launch an OpenFileDialog from a method binded to the Click of a button, but I get a Security Exception, which suggest that the call was not made from user initiated code, is there a way to manage this without using the button_Click() in the CodeBehind of the View? I am binding the method this way:


<Button Content="Pick File" Height="23" Name="btnPickFile"  Margin="15,0,0,0" >
                    <i:EventTrigger EventName="Click">
                        <cal:ActionMessage MethodName="UploadButton" />


thanks in advance for your help.

Mar 14, 2011 at 6:24 PM

Can you provide the code for your action. There may be something else in there that is causing the problem.

Mar 14, 2011 at 7:16 PM

part of my code on ViewModel is:


public void UploadButton()
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "JPEG files|*.jpg";
        if (openFileDialog.ShowDialog() == true) //at this part the exception is thrown
          Stream stream = (Stream)openFileDialog.File.OpenRead();
          byte[] bytes = new byte[stream.Length];
          stream.Read(bytes, 0, (int)stream.Length);

the resting code is not relevant.


Mar 14, 2011 at 8:05 PM

There's something else going on then. I've just confirmed that that works fine in a simple solution.

Mar 14, 2011 at 8:30 PM

I've already discovered the cause of my error, it was a typical case of UTFH...

I was not reading this part:

"When you are using the Visual Studio debugger with a dialog box, a SecurityException will be thrown if you set a breakpoint between dialog box creation and showing the dialog box"

I hope this little tip helps someone else.


Thanks for your attention

Mar 14, 2011 at 8:43 PM

That's good to know. Thanks for updating this thread!