locked
Dialogs must be user-initiated... RRS feed

  • Question

  • Hello,

    I use the following code which should avoid the "Dialogs must be user-initiated" error:

    in the Created event:

                    AddHandler Me.FindControl("cmdDokument_Laden").ControlAvailable, AddressOf OpenFileDialogButtonOpen_ControlAvailable

    then:

           Private Sub OpenFileDialogButtonOpen_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
                ' Handler um das Clickvent aus dem Browser zu erhalten
                AddHandler DirectCast(e.Control, Button).Click, AddressOf OpenFileDialogButtonOpen_Click
            End Sub
    
            Private Sub OpenFileDialogButtonOpen_Click(sender As Object, e As RoutedEventArgs)
                'Im Browser muss click event ausgelöst werden sonst darf man den Dialog nicht öffnen
                Dim files As New List(Of FileInfo)()
                Dim oFileInfo As FileInfo
    
                Dispatchers.Main.BeginInvoke(Sub()
                                                 Dim dlg = New OpenFileDialog()
                                                 dlg.Multiselect = True
                                                 dlg.Filter = "Dokumente|*.*;*.*"
    
                                                 If dlg.ShowDialog().GetValueOrDefault() Then
                                                     files = dlg.Files.ToList()
                                                 End If
    
                                                 For Each oFileInfo In files
    
                                                     Dim data = New Byte(oFileInfo.Length - 1) {}
                                                     Using stream = oFileInfo.OpenRead()
                                                         stream.Read(data, 0, data.Length)
                                                     End Using
                                                     Dim dokumentname = Path.GetFileNameWithoutExtension(oFileInfo.Name)
                                                     Dim extension = Replace(oFileInfo.Extension, ".", "")
                                                     Dim doc = Me.Pfuscheraktinhalt.AddNew()
                                                     doc.Dokumentname = dokumentname
                                                     doc.Dokumenttyp = Me.DataWorkspace.PDBData.Dokumenttyp.Where(Function(dokumenttyp) dokumenttyp.Dokumenttypextension.Equals(extension)).Single()
                                                     doc.Dokumentinhalt = data
    
                                                 Next
                                             End Sub)
            End Sub

     

    but I get still the error in line:

    If dlg.ShowDialog().GetValueOrDefault() Then

    why?

    robert



    • Edited by Mad Rian Wednesday, February 20, 2013 12:22 PM
    Wednesday, February 20, 2013 12:20 PM

Answers

  • Don't dispatch to the main dispatcher within the click event handler because you are already on the main thread. Then this code:

    Dim doc = Me.Pfuscheraktinhalt.AddNew()
    doc.Dokumentname = dokumentname
    doc.Dokumenttyp = Me.DataWorkspace.PDBData.Dokumenttyp.Where(Function(dokumenttyp) dokumenttyp.Dokumenttypextension.Equals(extension)).Single()
    doc.Dokumentinhalt = data

    should be dispatched to the dispatcher of the screen:

    this.Details.Dispatcher.BeginInvoke(Sub()
        Dim doc = Me.Pfuscheraktinhalt.AddNew()
        doc.Dokumentname = dokumentname
        doc.Dokumenttyp = Me.DataWorkspace.PDBData.Dokumenttyp.Where(Function(dokumenttyp) dokumenttyp.Dokumenttypextension.Equals(extension)).Single()
        doc.Dokumentinhalt = data
    End Sub)
    You should only access data on the dispatcher to which the data belongs, otherwise you'll run into trouble later with invalid cross threading problems.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Angie Xu Monday, March 11, 2013 12:46 AM
    Thursday, February 21, 2013 10:47 AM
    Moderator

All replies

  • Hello,

    Please can anybody from the LS Team give me an answer if it is a bug or do I something wrong because my solution works somethimes and sometimes the message "Dialogs must be user-initiated" comes up...

    robert

    Thursday, February 21, 2013 9:37 AM
  • Don't dispatch to the main dispatcher within the click event handler because you are already on the main thread. Then this code:

    Dim doc = Me.Pfuscheraktinhalt.AddNew()
    doc.Dokumentname = dokumentname
    doc.Dokumenttyp = Me.DataWorkspace.PDBData.Dokumenttyp.Where(Function(dokumenttyp) dokumenttyp.Dokumenttypextension.Equals(extension)).Single()
    doc.Dokumentinhalt = data

    should be dispatched to the dispatcher of the screen:

    this.Details.Dispatcher.BeginInvoke(Sub()
        Dim doc = Me.Pfuscheraktinhalt.AddNew()
        doc.Dokumentname = dokumentname
        doc.Dokumenttyp = Me.DataWorkspace.PDBData.Dokumenttyp.Where(Function(dokumenttyp) dokumenttyp.Dokumenttypextension.Equals(extension)).Single()
        doc.Dokumentinhalt = data
    End Sub)
    You should only access data on the dispatcher to which the data belongs, otherwise you'll run into trouble later with invalid cross threading problems.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Angie Xu Monday, March 11, 2013 12:46 AM
    Thursday, February 21, 2013 10:47 AM
    Moderator
  • Hello Justin,

    seems that it works now but have to test it in production because in our last version we have done this with that solution and sometimes the error pops up...

    robert

    Thursday, February 21, 2013 5:19 PM