3

我的代码有问题。我在我的项目中使用 OpenFileDialog,当我调用 ShowDialog 方法时会引发异常。我不明白为什么。

    private void open_FileMenu(object sender, RoutedEventArgs e)
    {
        OpenFileDialog browser = new OpenFileDialog();
        browser.AddExtension = true;
        browser.Filter = "Audio, Video File | *.wma; *.mp3; *.wmv";
        browser.Title = "Choose your file";
         if (browser.ShowDialog() == System.Windows.Forms.DialogResult.Yes) // Exception thrown here
          {
            try
            {
                string FileName = browser.FileName;
                MyMedia.Source = new Uri(FileName);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

这个例外说

    A first chance exception of type 'System.ComponentModel.Win32Exception' occurred  in WindowsBase.dl

附加信息:参数不正确

有人可以帮助我吗?

4

2 回答 2

4

In WinForms CommonDialog.ShowDialog() comes from System.Windows.Forms.dll and returns a DialogResult.

In WPF CommonDialog.ShowDialog() comes from PresentationFramework.dll and returns a bool?

Naturally this leads to a lot of confusion. Ultimately you want this instead.

if (browser.ShowDialog() == true)
于 2013-02-08T15:47:54.073 回答
1

这对我有用:

        OpenFileDialog browser = new OpenFileDialog();
        browser.AddExtension = true;
        browser.Filter = "Audio, Video File | *.wma; *.mp3; *.wmv";
        browser.Title = "Choose your file";
        string FileName;
        bool? res = browser.ShowDialog(); // No exception thrown here
        if (res ?? false)
        {
            try
            {
                 FileName = browser.FileName;
                //MyMedia.Source = new Uri(FileName);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
于 2013-02-08T15:23:45.047 回答