Silverlight 中的 securityException:对话框必须由用户启动
我正在尝试创建一个 Silverlight 应用程序来下载通过 URL 访问的文件。我尝试使用 WCF,但现在我尝试使用 Webclient 来完成此操作。 当我尝试使用此示例代码时,出现 securityException 错误,提示“对话框必须由用户启动”。 有人可以解释一下我做错了什么吗?谢谢。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
#region Constructors
public MainPage()
{
InitializeComponent();
// SaveFileDialog sfd = new SaveFileDialog();
}
#endregion
#region Handlers
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if ((bool)sfd.ShowDialog())
{
StreamReader sr = new StreamReader(e.Result);
string str = sr.ReadToEnd();
StreamWriter sw = new StreamWriter(sfd.OpenFile());
sw.Write(str);
}
}
private void download_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg", UriKind.Absolute));
}
#endregion
}
}
I'm trying to create a Silverlight application that downloads a file accessed by a URL. I tried using WCF, but now I'm attempting to do it with Webclient.
When I try using this sample code, I'm getting a securityException error that says "Dialogs must be user initiated."
Can someone please explain what I'm doing wrong? Thanks.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
#region Constructors
public MainPage()
{
InitializeComponent();
// SaveFileDialog sfd = new SaveFileDialog();
}
#endregion
#region Handlers
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if ((bool)sfd.ShowDialog())
{
StreamReader sr = new StreamReader(e.Result);
string str = sr.ReadToEnd();
StreamWriter sw = new StreamWriter(sfd.OpenFile());
sw.Write(str);
}
}
private void download_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg", UriKind.Absolute));
}
#endregion
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
旧问题的答案指出的基本问题是 OpenReadCompleted 事件与按钮单击异步发生。 silverlight 无法确保事件处理程序中执行的代码是用户操作的结果。
要扭转局面,需要让用户在调用下载之前选择保存目的地。这是一个处理很多事情的按钮单击事件: -
顺便说一句,如果您成功使用 StreamReader / StreamWriter ,很可能会把事情搞砸,这些是用于读取和写入文本的。在这种情况下,只需调用
CopyTo
即可解决问题。The basic problem as the answers to the older questions point out is that the
OpenReadCompleted
event occurs asynchronously with the button click. There is no way for silverlight be be sure that code execting in the event handler is a result of a user action.What is needed to turn things around, get the user to pick the save destination before invoking the download. Here is a button click event that handles the lot:-
BTW had you been successfull your use of StreamReader / StreamWriter may well have messed things up, these are for reading and writing text. In this case a simple call to
CopyTo
does the trick.