Silverlight 3 中的 SaveFileDialog 问题

发布于 2024-08-06 06:38:37 字数 3060 浏览 3 评论 0原文

我在 Silverlight 3 中的 SaveFileDialog 遇到了奇怪的异常。我真的不知道问题出在哪里。

我在用户控件的 Loaded 事件中创建了 SaveFileDialog 实例。单击“Download”按钮且“dialogResult”为“true”后,开始异步文件下载。文件下载完成后,调用方法OpenFile()。一次工作正常,但第二次我收到异常:

异常消息:

"No file was selected"

详细信息:

{System.InvalidOperationException: No file was selected.
   at System.Windows.Controls.SaveFileDialog.OpenFile()
   at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)}

堆栈:

   at System.Windows.Controls.SaveFileDialog.OpenFile()
   at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)

这里代码片段:

private void _userControlFileDialog_Loaded(object sender, RoutedEventArgs e)
        {
            _comboBoxVersions.ItemsSource = _file.Versions;
            if (_comboBoxVersions.Items.Count > 0)
                _comboBoxVersions.SelectedIndex = 0;

            String extension = "*." + _file.Extension;

            _sfd = new SaveFileDialog();
            _sfd.DefaultExt = _file.Extension;
            _sfd.Filter = extension + "|" + extension;
        }

        private void _hyperlinkButtonDownload_Click(object sender, RoutedEventArgs e)
        {
            string path = ((FileVersion)_comboBoxVersions.SelectedItem).Url;

            bool? dialogResult = _sfd.ShowDialog();

            if (dialogResult == true)
            {
                AppContext.BL.DownloadFileCompleted += new Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler(BL_DownloadFileCompleted);
                AppContext.BL.DownloadFileAsync(AppContext.AuthenticatedUser, path);
            }
        }

        void BL_DownloadFileCompleted(object sender, Spaces.Client.BL.Interface.EventArguments<byte[]> e)
        {
            byte [] data = e._result;

            using (Stream fileStream = (Stream)_sfd.OpenFile())
            {
                fileStream.Write(data, 0, data.Length);
                fileStream.Flush();
                fileStream.Close();
            }

        }

有人知道出了什么问题吗?

问候 安东·卡尔西克

i have weird exception by SaveFileDialog in Silverlight 3. I don't really have a idea where the problem is.

I create instance of SaveFileDialog in Loaded event of user control. After Download button is clicked and dialogResult is true asynchronous file download is started. After file download is completed, method OpenFile() is called. This works fine once, but second time I get exception:

Exception message:

"No file was selected"

Details:

{System.InvalidOperationException: No file was selected.
   at System.Windows.Controls.SaveFileDialog.OpenFile()
   at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)}

Stack:

   at System.Windows.Controls.SaveFileDialog.OpenFile()
   at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
   at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)

Here code snippet:

private void _userControlFileDialog_Loaded(object sender, RoutedEventArgs e)
        {
            _comboBoxVersions.ItemsSource = _file.Versions;
            if (_comboBoxVersions.Items.Count > 0)
                _comboBoxVersions.SelectedIndex = 0;

            String extension = "*." + _file.Extension;

            _sfd = new SaveFileDialog();
            _sfd.DefaultExt = _file.Extension;
            _sfd.Filter = extension + "|" + extension;
        }

        private void _hyperlinkButtonDownload_Click(object sender, RoutedEventArgs e)
        {
            string path = ((FileVersion)_comboBoxVersions.SelectedItem).Url;

            bool? dialogResult = _sfd.ShowDialog();

            if (dialogResult == true)
            {
                AppContext.BL.DownloadFileCompleted += new Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler(BL_DownloadFileCompleted);
                AppContext.BL.DownloadFileAsync(AppContext.AuthenticatedUser, path);
            }
        }

        void BL_DownloadFileCompleted(object sender, Spaces.Client.BL.Interface.EventArguments<byte[]> e)
        {
            byte [] data = e._result;

            using (Stream fileStream = (Stream)_sfd.OpenFile())
            {
                fileStream.Write(data, 0, data.Length);
                fileStream.Flush();
                fileStream.Close();
            }

        }

Have anybody idea what is wrong?

Regards
Anton Kalcik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

猫瑾少女 2024-08-13 06:38:37

多个事件处理程序存在问题。每次单击时都会附加事件处理程序,并且永远不会分离。 UserControl 关闭后,事件处理程序仍保持附加状态。因此,开发人员需要以正确的方式分离事件处理程序。

问候
阿卡

There was problem with multiple event handlers. On each click is event handler attached and never detached. Event handler stays attached also after UserControl is closed. So it is on developer to detach event handler on properly way.

Regards
AKa

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