Reputation: 13068
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 download task. The flow goes this way:
I am using the following code in a function which is called from the OnCompleted event handler to accomplish SaveFileDialog prompt and then streaming to file.
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "JPG Files|*.jpg" + "|All Files|*.*";
bool? dialogResult = dialog.ShowDialog();
if (dialogResult == true)
{
using (Stream fs = (Stream)dialog.OpenFile())
{
fs.Write(e.Result, 0, e.Result.Length);
fs.Close();
}
}
The SaveFileDialog would throw the error "Dialogs must be user-initiated." when invoking ShowDialog method in the above code. What could i be missing here? How to overcome this?
Upvotes: 18
Views: 32577
Reputation: 11
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
Upvotes: 1
Reputation: 21
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.
Upvotes: 1
Reputation: 5665
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)
Upvotes: 1
Reputation: 3343
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.
Upvotes: 20
Reputation: 15015
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!
Upvotes: 5