对话框必须是用户启动的问题和异步。要求

时间:2012-07-11 17:34:06

标签: c# silverlight silverlight-5.0

我得到“”对话必须是用户启动的“错误。

有任何解决方法可以解决吗?

谢谢!

private void ExportDataToCSV()
        {
            string separator = GetSeparator();

            LoadClientContentItemStatistics(); // async. request of MVVM Light Toolkit

            foreach (var item in this._view)
            {
                sb.AppendLine("\"" + item.Player.PlayerGroup.Name + "\"" + separator + "\"" + item.Player.Name + "\"" + separator + "\"" + item.ClientContentItem.Name + "\"" + separator + "\"" + item.TotalTime.ToString() + "\"" + separator + "\"" + item.TotalQuantity.ToString() + "\"" + separator + Environment.NewLine);
            }
            SaveFileDialog sfd = new SaveFileDialog()
            {
                DefaultExt = "csv",
                Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                FilterIndex = 1
            };
            if (sfd.ShowDialog() == true) // "Dialogs must be user-initiated" ERROR
            {
                using (Stream stream = sfd.OpenFile())
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(sb.ToString());   
                        writer.Close();
                    }
                    stream.Close();
                }
            }

            IsExport = true;
        }   

1 个答案:

答案 0 :(得分:3)

此错误意味着它所说的内容:用户必须启动此对话框。因此,您只能从用户点击事件或其他用户发起的事件中调用此方法。