Silverlight 中的 securityException:对话框必须由用户启动

发布于 2024-11-28 12:34:49 字数 1644 浏览 1 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-12-05 12:34:49

旧问题的答案指出的基本问题是 OpenReadCompleted 事件与按钮单击异步发生。 silverlight 无法确保事件处理程序中执行的代码是用户操作的结果。

要扭转局面,需要让用户调用下载之前选择保存目的地。这是一个处理很多事情的按钮单击事件: -

private void Button_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    if (sfd.ShowDialog() ?? false)
    {

        WebClient client = new WebClient();
        client.OpenReadCompleted += (s, args) =>
        {
            using (Stream output = sfd.OpenFile())
            {
                args.Result.CopyTo(output);
                output.Flush();
            }
        };
        client.OpenReadAsync(new Uri("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg", UriKind.Absolute));
    }
}

顺便说一句,如果您成功使用 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:-

private void Button_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    if (sfd.ShowDialog() ?? false)
    {

        WebClient client = new WebClient();
        client.OpenReadCompleted += (s, args) =>
        {
            using (Stream output = sfd.OpenFile())
            {
                args.Result.CopyTo(output);
                output.Flush();
            }
        };
        client.OpenReadAsync(new Uri("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg", UriKind.Absolute));
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文