【问题标题】:OpenFileDialog causes WPF app to crashOpenFileDialog 导致 WPF 应用程序崩溃
【发布时间】:2012-12-01 03:31:43
【问题描述】:

在我的WPF 应用程序中,我使用OpenFileDialog 选择图像并将其加载到应用程序中,这可以正常工作。

但如果我从闪存驱动器运行相同的应用程序,则在 UI 冻结之后加载图像,任何点击 UI 都会导致应用程序崩溃。

我也有管理员manifest 来应用程序。

【问题讨论】:

标签: c# wpf openfiledialog savefiledialog filedialog


【解决方案1】:

我找不到很好的解释,但我解决了这个问题,使用有效的本地路径设置 InitialDirectory(例如,Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

【讨论】:

    【解决方案2】:

    我之前从网络驱动器运行时看到过类似的情况。如果应用程序不是从完全受信任的来源加载的,您可能会收到 SecurityException。

    无论如何,请尝试在代码周围添加一个 try/catch 块,以查看您是否遇到异常。

    【讨论】:

    • 这里我没有收到安全异常。
    【解决方案3】:

    在这种情况下OpenFileDialog 会导致应用挂起和崩溃。

    因此将OpenFileDialog 移至新线程。一切正常。

    【讨论】:

    • 此线程问题也发生在 Windows 窗体中:您的应用在调试单击事件处理程序时可能会崩溃,该处理程序调用保存或打开对话框,并且 Visual Studio 在输出窗口中触发:A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Windows.Forms.dll
    【解决方案4】:

    我发现这个问题不仅出现在 WPF 中(崩溃),而且出现在 WinForms 中。 很难说问题的根源是什么,但似乎与 OpenFileDialog 相关的 Microsoft dll 有错误(对我来说,它是 CmnDlg32.dll)

    我可以调用 ShowDialog() 函数的唯一方法是将它包装在事件中并在

    的帮助下调用
    this.BeginInvoke(
            new Action<YourObject, EventArgs>(YourObject_FileDialogOpened), new object[] 
                                                            { YourObjectInstance, e });
    

    其中“this”是一个控件(例如,表单)。

    BeginInvoke(...) 您调用的授权将以适当的方式进行处理。

    如果您在按钮单击事件或任何其他类似情况下使用 OpenFileDialog 调用,则不会出现问题。

    【讨论】:

      【解决方案5】:

      使用 OpenFileDialog 时 Winform 如何崩溃

      using(var ofd = new OpenFileDialog())
      {
         ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
         if(ofd.ShowDialog() == DialogResult.OK) // <-- reason of crashing
         {
           PictureBox.Image = Image.FromFile(ofd.FileName);
         }
      }
      

      如何解决问题

      using(var ofd = new OpenFileDialog())
      {
         ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
         DialogResult Action = a.ShowDialog();
         if(Action == DialogResult.OK) // <-- To fix
         {
           PictureBox.Image = Image.FromFile(ofd.FileName);
         }
      }
      

      【讨论】:

        【解决方案6】:

        使用这样的东西:

        Dispatcher.Invoke(new Action(() =>
                    {
                        using (SaveFileDialog fd = new SaveFileDialog())
                        {
                            var json = JsonConvert.SerializeObject(arScene, Formatting.Indented);
        
                            var bytes = UTF8Encoding.UTF8.GetBytes(json); // or any byte array data
        
                            fd.Filter = "JSon files (*.json)|*.json|All files (*.*)|*.*|ARScene (*.ARScene)|*.ARScene";
                            fd.Title = "Save an ARScene File";
                            fd.AutoUpgradeEnabled = true;
                            fd.DefaultExt = "ARScene";
                            fd.OverwritePrompt = false;
                            fd.RestoreDirectory = true;
                            fd.SupportMultiDottedExtensions = true;
                            fd.CreatePrompt = false;
        
                            if (fd.ShowDialog() == DialogResult.OK)
                            {
                                if (fd.FileName != "")
                                {
                                    FileStream fs = (FileStream)fd.OpenFile();
                                    if (fs != null)
                                    {
                                        fs.Write(bytes, 0, bytes.Length);
                                        fs.Close();
                                    }
        
                                }
                            }
                            fd.Dispose(); // not needed, but save;-)
                        }
        }));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-21
          • 2016-06-04
          • 2011-08-24
          • 2014-05-03
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          相关资源
          最近更新 更多