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

前端 未结 5 1531
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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
    

提交回复
热议问题