"对话框必须由用户发起的&QUOT。与SaveFileDialog Silverlight 3中对话框、用户、QUOT、SaveFileDialog

2023-09-02 10:32:27 作者:孤独成病

我工作的一个Silverlight 3应用程序用C#。我想允许用户从Silverlight应用程序下载图像。我使用SaveFileDialog执行文件下载任务。流程进入这样:

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:

在用户点击的SL应用程序下载按钮。 在Web服务调用调用以从服务器的映像 OnCompleted异步Web方法调用的事件处理程序被调用,并从服务器接收二进制图像 在OnCompleted事件处理SaveFileDialog提示用户将图像保存到计算机中。 流的图像文件在用户的硬盘。

我使用下面的code的这是从OnCompleted事件处理函数调用来完成SaveFileDialog提示功能,然后分流到文件。

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();
                }
            }

在SaveFileDialog会抛出错误对话框必须为用户启动。在上述code调用ShowDialog方法时。我能来这里丢失?如何克服呢?

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?

推荐答案

这是什么错误信息的意思是,你只可以响应用户发起的事件,显示SaveFileDialog如按钮的点击。在你描述的例子,你是不是显示SaveFileDialog响应点击,而是针对一个完整的HTTP请求(这是不被认为是用户发起的事件)。所以,你需要做的就是这个工作的,在HTTP请求的完成情况,表现出一定的UI来表示用户下载完成后,点击此处将文件保存到您的计算机,当用户点击对此消息,显示SaveFileDialog。

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.

 
精彩推荐