1

我得到 en ""Dialogs must be user-initiated" 错误。

任何线索如何解决?

谢谢!

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;
        }   
4

1 回答 1

3

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

于 2012-07-11T17:39:30.997 回答