none
SL3 openfile dialog

    Question

  •  The following code was working with SL2 no longer working with SL3

     

    The error messase did append at the ShoDialog instruction and the message is "Dialogs must be user-initiated."

     

    OpenFileDialog dlg = new OpenFileDialog(); // new instance
                dlg.Multiselect = false;
                dlg.Filter = "xlm|*.xml";
                if ((bool)dlg.ShowDialog())
                {
                    UploadFile(dlg.File.Name, dlg.File.OpenRead());

                }
                else
                {
                    MessageBox.Show("No files selected");
                }

     

    Saturday, March 21, 2009 1:20 PM

Answers

  • I had the same problem - but it was because I was debugging the application and I have the breakpoint on the ShowDialog() line.

     When I moved it to a line after it, it was fine.

    Also, I'm not sure if it makes any difference, but put it into an event that has RoutedEventArgs as the parameter and see if it works...

     :-)

    Tuesday, March 31, 2009 1:59 PM
  • This happened to me too. I wanted to open a file in the button click event handler. I had a breakpoint on showdialog and the debugger threw an exception [error 4004 silverlight "dialogs must be user-initiated"]. I just moved the breakpoint to the next line and SL did not throw any exception

    Tuesday, April 21, 2009 7:59 PM
  • Simply, for the OFD.ShowDialog to work correctly, it must be called from a user initiated event.
    Button.Click is one simple example.

    Also, you will get an exception "Dialogs must be user-initiated" if you try to call "ShowDialog" under the debugger.

    Wednesday, April 22, 2009 12:13 PM

All replies

  • Hi! If it worked before, it shouldn't have. By design they want things like Open/Save FileDialogs, FullScreen, and IsolatedStorage increasings user-initiated. You can have to have a button's* event handler open that dialog box, or else Silverlight will (and should) ignore it.

    * Doesn't have to be a "button." Any object that the user can click or press a key to initiate some kind of action.
    Saturday, March 21, 2009 2:29 PM
  • Thank for the info. It will help me

    Here the Xaml where the Click event occur.

     

    <dxm:AgMenu VerticalAlignment="Top" Background="{StaticResource ShinyExpanderYellow}" >
                        <dxm:AgMenuItem Header="File" HorizontalAlignment="Stretch">
                            <dxm:AgMenuItem Header="New From" Click="NewFrom" ></dxm:AgMenuItem>

    •                         <dxm:AgMenuItem Header="Read"  Click="Read"> </dxm:AgMenuItem>

                            <dxm:AgMenuItem Header="Create" Click="Create"></dxm:AgMenuItem>                      
                        </dxm:AgMenuItem>
                    </dxm:AgMenu>

     

    I did replace the Read by Read_Click and change the code behind evnet name. Still not working.

    I did make another example with a button and it does work.

    The problem might come from the  age menu control (Header). I will post probably the question to there site.

    By the time I will check I can add a button in the header to solve the problem.


    Here the application link I'm developing in SL2 if you still have SL2 installed. open the menu and Click the read button. I does work.

    http://mpeapps.com/SilverlightLoadsheetLayout1TestPage.aspx

     

    Saturday, March 21, 2009 6:08 PM
  •  Did remove the age menu control and put only a button. It's not working. Maybe it's from the project upgrade tool that may have corrupt my project from SL2 to SL3. I'm not sure.

    I will create a new projet a built it from scratch.

    Sunday, March 22, 2009 4:54 AM
  • I had the same issue with filedialog.ShowDialog method but found that as soon as I moved the actual filedialog.ShowDialog call to the button_click handler it worked fine. Previously I was calling a method from the handler which contained the ShowDialog code.  Hope this helps.

    Tuesday, March 24, 2009 10:10 PM
  • Normal 0 21 false false false FR X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

    Normal 0 21 false false false FR X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

    Sorry for the text above I try to paste the image. I can't remove it.

    Thanks for the reply

     I did the same as you mentionned above on a small project and it does work fine but I would like to understand why it's not working on my bigger project. I'm a beginner.

     

    Here the details of the error. May be someone knows the problem and can tell me the fix.

     

    System.Security.SecurityException was unhandled by user code

      Message="Dialogs must be user-initiated."

      StackTrace:

           à System.Windows.Controls.OpenFileDialog.ShowDialog()

           à SilverlightLoadsheetLayout1.LoadsheetPage.AgMenuItem_Click(Object sender, EventArgs e)

           à DevExpress.AgMenu.AgMenuItem.RaiseClick()

           à DevExpress.AgMenu.AgMenuItem.DoClick()

           à DevExpress.AgMenu.AgMenuItem.PerformClick()

           à DevExpress.AgMenu.AgMenuItem.OnMouseLeftButtonUp(Object sender, MouseEventArgs e)

           à System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)

           à MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

      InnerException:

    Friday, March 27, 2009 6:01 PM
  • I had the same problem - but it was because I was debugging the application and I have the breakpoint on the ShowDialog() line.

     When I moved it to a line after it, it was fine.

    Also, I'm not sure if it makes any difference, but put it into an event that has RoutedEventArgs as the parameter and see if it works...

     :-)

    Tuesday, March 31, 2009 1:59 PM
  • This happened to me too. I wanted to open a file in the button click event handler. I had a breakpoint on showdialog and the debugger threw an exception [error 4004 silverlight "dialogs must be user-initiated"]. I just moved the breakpoint to the next line and SL did not throw any exception

    Tuesday, April 21, 2009 7:59 PM
  • Simply, for the OFD.ShowDialog to work correctly, it must be called from a user initiated event.
    Button.Click is one simple example.

    Also, you will get an exception "Dialogs must be user-initiated" if you try to call "ShowDialog" under the debugger.

    Wednesday, April 22, 2009 12:13 PM
  • I have very simple program in SL3. Only one button and in handler for click on button only creating of OFD and show.

    There is not any breakpoint in program.

    The same code a few times run ok and then a few times fail.

    There have to be another  resaon for problems with OFD!

    <UserControl x:Class="SL3.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="LayoutRoot">

    <Button Content="butonek" Width="60" Height="30" Click="Button_Click"/>

    </Grid>

    </UserControl>

    --------------------------

    private void Button_Click(object sender, RoutedEventArgs e)

    {

    OpenFileDialog dlg = new OpenFileDialog();

    bool? userClickedOK = dlg.ShowDialog();

    if (userClickedOK == true)

    {

    string s;

    try

    {

    }

    catch (Exception ex)

    {

    s = ex.Message;

    }

    }

    }

    Thursday, July 16, 2009 7:13 AM
  • What enviornment do you have? OS/Browser/...etc

    How many times does it run fine before it fails? and what do you mean by "Fail"? doesn't save, dialog doesn't open, etc.

    Thursday, July 16, 2009 12:19 PM
  • OS:   MS Windows Server 2003 SP2

    Browser:   IE 7 (7.0.5730.13)

    Visual Studio Team System 2008 / C#

    I have installed SL3 release over SL3B withou any warning or error (Can I somehow check if I have installed right DLL and another files?)

    Fail means show error : "Dialogs must be user-initiated." instead of open file dialog.
    It looks like random process. Number of errors and successes is very changeable.

     Thank You for answer

     

     

    Friday, July 17, 2009 5:57 AM
  •  i'm having the same problem. it works fine on my windows machine, but doesn't work at all on the mac os x. i can click the button but nothing at all happens. i've tried it on both firefox and safari. mac os x version 10.5.7, intel core duo.

     need a solution to this ASAP, thanks.

    Thursday, July 30, 2009 2:51 PM
  •  actually, never mind, it works fine. here was my problem. for the Filter property, i had this line of code:

     ofd.Filter = "TGA File (*.tga)|*.tga;";

     that last semi-colon right after the *.tga was messing it up. i removed it and it started to work! weird but im just glad its working now =)

     

    Thursday, July 30, 2009 3:08 PM
  • There's another little gotcha to be aware of. As well as the open file dialog having to be in the event handler, any other dialog before the OFD will break the code.

    In my example, I have some code which checks for changes before displaying the OFD to warn the user above unsaved edits. This worked in SL2, but if a messagebox is shown before the OFD, the error occurs. Not sure of a work around, but removing the messagebox.show before the OFD solves the error.

    Example based on the code which broke for me (stuff removed for breivity).

     

    private void btnImport_Click(object sender, RoutedEventArgs e)

    {

    if (isDirty == true)

    {

    //ask if there are sure

    MessageBoxResult result = MessageBox.Show("If you import a new file, any existing route will be removed.", "Import and lose existing data?", MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.Cancel)

    return;

    }

     

    //display a UI to grab a file

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Filter = "GPX Files|*.gpx";

    dlg.Multiselect = false;if (dlg.ShowDialog() == true)

    {

     //open file etc

    }

     

    Thursday, August 06, 2009 11:49 AM
  • Why does Microsoft think they can or should dictate when an independant 3rd party applicaiton will open the File Open dialog?  The need to open the dialog could be as a result of a server side call, or as a result of a non-user event.  In our paticular case, we display a shadow over a window to indicate that it cannot take any input.  We display a form over top of that shadow to create a modal form.  When the form closes [which is a user event] we take down the shadow, and wait for an event to be triggered by the shadow form closing.  When that event fires, which is not a user event, we display the Open file Dialog.  This worked very well in SL 2, now it no longer works.

    This kind of presumption and enforcement will be the downfall of Microsoft.  I am a 25 year veteran of MS technologies, and I am ready to switch to any vendor who will provide great tools and leave the design choices to the developers.

    MS came to power by opening doors for developers, not closing them.

    Wednesday, September 09, 2009 8:57 AM
  • Me too.

    I Have one simple silverlight application, a circle, a rectangle, and two charts on canvas. and a button with click event to save a file.

    When the canvas size is big enought(exceed the chart size), every time I click the button, the error " dialogs must be user initiated" will occur. When I reduced the canvas size, the first time I click the button, it works as expected, but the second time I click the button, the same error will occur again!

    Definitely don't know how to solve this problem.

    Wednesday, September 09, 2009 3:09 PM
  • Aparently you cant have a breakpoint in any line of code of the the handler method that opens the save file dialog or open file dialog.
    Tuesday, October 20, 2009 2:43 PM
  • I have found that as long as you stick the breakpoint after the open dialog, everything seems to work correctly. It would be nice to allow the application to open/save file through use of .Net permissions which could be controlled by the user.

    Tuesday, October 20, 2009 4:31 PM
  • Pal, I figured out the solution to the file dialog Security Exception problem. It is a temporary workaround. Just do an IISreset. Someone in the forum suggested using "RoutedEventArgs" as the second parameter in the button click event.  I dont think this has anything to do with the bug. Both "EventArgs" and "RoutedEventArgs" work fine! Just ran a few tests. Hope this helps some bloke! Take care, VJ Koganti, United Kingdom.

    Tuesday, November 24, 2009 6:36 PM
  • It's the same for me, just Rebuild All or stop the Development Server and back to work

    Thursday, December 17, 2009 6:24 AM
  • I just cleaned and rebuilt my project an the error went away.  Too bad it took an hour to find this post.

    Tuesday, October 05, 2010 4:30 PM
  • Thanks for the post, I did a clean and re-build and that did the trick!

    Again, thank you...

    Tuesday, October 26, 2010 5:02 PM