launching an OpenFileDialog

Mar 14, 2011 at 4:01 PM

hi,

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:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <cal:ActionMessage MethodName="UploadButton" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>

 

thanks in advance for your help.

Coordinator
Mar 14, 2011 at 5: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 6: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.

          

Coordinator
Mar 14, 2011 at 7: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 7: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

Coordinator
Mar 14, 2011 at 7:43 PM

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