“Dialogs must be user-initiated.” with SaveFileDialog in Silverlight 3

前端 未结 5 1517
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 11:56

I am working on a Silverlight 3 app with C#. I would like to allow the user to download an image from the Silverlight app. I am using SaveFileDialog to perform the file down

相关标签:
5条回答
  • 2020-12-10 12:28

    How about asking first, before downloading? It seems to suggest from the error message that it is the way Silverlight wants you to prompt to ensure it knows a user requested the action, not you spaming the user with popups.

    Silverlight security model aside, I'd rather not wait for a download to finish before being asked where to put it!

    0 讨论(0)
  • 2020-12-10 12:31

    I just started on Silverlight 4 and had the same issue. It seems that if you manually create event handlers, the security exception is thrown, even if the event handler is handling a button click event with the correct parameters, but if you use the "create a new event handler" option on the button in Xaml under the click event, the new event handler, with the same code and parameters now works....this is one of the many "corky" things that I have come across since starting the transition from WPF to Silverlight.

    0 讨论(0)
  • 2020-12-10 12:33
    Private _syncContext As SynchronizationContext
    Private mBigStream As Stream
    
     Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Try
            Dim saveDialog As New SaveFileDialog
    
            saveDialog.Filter = "Word |*.doc"
            saveDialog.DefaultExt = ".doc"
    
            If saveDialog.ShowDialog() Then
                Try
                    mBigStream = saveDialog.OpenFile()
    
                    _syncContext = SynchronizationContext.Current
    
                    oWebService.GetReportAsync(Params, ... , _syncContext)
                Catch ex As Exception
                    MessageBox.Show("File busy.")
                End Try
            End If
        Catch ex As Exception
            LogError((New System.Diagnostics.StackTrace()).GetFrame(0).GetMethod().Name.ToString, Err.Description)
        End Try
    End Sub
    
    Private Sub oWebService_GetReportCompleted(sender As Object, e As MainReference.GetReportCompletedEventArgs) Handles oWebService.GetReportCompleted
        Try
            ' e.Result is byte()
    
            If e.Result IsNot Nothing Then
                If e.Result.Count > 0 Then
                    _syncContext.Post(Sub()
                                          Try
                                              mBigStream.Write(e.Result, 0, e.Result.Length)
    
                                              mBigStream.Flush()
                                              mBigStream.Close()
    
                                              mBigStream.Dispose()
    
                                              mBigStream = Nothing
                                          Catch ex As Exception
                                              LogError((New System.Diagnostics.StackTrace()).GetFrame(0).GetMethod().Name.ToString, Err.Description)
                                          End Try
                                      End Sub, Nothing)
    
                    _syncContext = Nothing
                End If
            End If
        Catch ex As Exception
            LogError((New System.Diagnostics.StackTrace()).GetFrame(0).GetMethod().Name.ToString, Err.Description)
        End Try
    End Sub
    
    0 讨论(0)
  • 2020-12-10 12:46

    What this error message means is that you can only show a SaveFileDialog in response to a user initiated event, such as a button click. In the example you describe, you are not showing SaveFileDialog in response to a click, but rather in response to a completed http request (which is not considered a user initiated event). So, what you need to do to get this to work is, in the Completed event of the http request, show some UI to the user saying "download completed, click here to save the file to your computer", and when the user clicks on this message, display the SaveFileDialog.

    0 讨论(0)
  • 2020-12-10 12:46

    As Keith mentioned this is by design. This tutorial gives an excellent example using code which I used to download a file from the server in the "correct" way. (Works in Silverlight 4 too)

    0 讨论(0)
提交回复
热议问题