错误:Application'是'System.Windows.Application'和'System.Windows.Forms.Application'之间的模糊引用

时间:2015-02-26 04:58:34

标签: c# visual-studio-2013

我在Visual Studio 2013中运行它。

对于Application.Current.Shutdown我得到:

'Application' is an ambiguous reference between 'System.Windows.Application' and 'System.Windows.Forms.Application'

我尝试使用using SystemInformation = System.Windows.Forms.SystemInformation;代替using System.Windows.Forms作为一个网站建议但我收到错误:

The type or namespace name 'OpenFileDialog' could not be found (are you missing a using directive or an assembly reference?)

OpenFileDialog dlg = new OpenFileDialog();

如何修复它以便我可以使用OpenFileDialog?我想要一个打开的文件对话框出现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PTDGUI.View;  

private void mnuExit_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Shutdown();
}

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.ShowDialog();

    if (dlg.ShowDialog() == DialogResult.OK)
    {
       string fileName;
       fileName = dlg.FileName;
       MessageBox.Show(fileName);
    }
}

1 个答案:

答案 0 :(得分:4)

尝试这样做可以避免Application类的歧义。

private void mnuExit_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Application.Current.Shutdown();
}

<强>更新

这种歧义的原因是命名空间System.Windows.FormsSystem.Windows都包含类Application的定义。所以在这一点上,c#编译器会混淆Application的哪个实现引用。

如果您没有引用System.Windows.Forms中的任何类,则可以将其从使用列表中删除。