[英]How do I use the methods of a class that comes with a C# namespace
I am a newbie to C#我是 C# 的新手
So I want to use the Microsoft.Win32 namespace to show a dialog using the ShowDialog method.所以我想使用 Microsoft.Win32 命名空间来显示一个使用 ShowDialog 方法的对话框。
Here is my code below:下面是我的代码:
using System;
using Microsoft.Win32;
public abstract class example
{
static void Main()
{
CommonDialog.ShowDialog();
}
}
I get the following error,我收到以下错误,
Class1.cs(11,9): error CS0103: The name 'CommonDialog' does not exist in the current context
Forgive my ignorance, but i simply do not know原谅我的无知,但我根本不知道
You need to add this to the project https://www.nuget.org/packages/PresentationFramework/您需要将此添加到项目https://www.nuget.org/packages/PresentationFramework/
Furthermore, CommondDialog is an abstract class.此外,CommodDialog 是一个抽象的 class。 Calling ShowDialog most likely will not work.
调用 ShowDialog 很可能不起作用。 It should not compile.
它不应该编译。 I am not sure how Microsoft.Win32 namespace works but if you are new to C# and want to learn about Windows Forms, I would suggest this: https://docs.microsoft.com/en-us/visualstudio/ide/create-csharp-winform-visual-studio?view=vs-2019
I am not sure how Microsoft.Win32 namespace works but if you are new to C# and want to learn about Windows Forms, I would suggest this: https://docs.microsoft.com/en-us/visualstudio/ide/create- csharp-winform-visual-studio?view=vs-2019
@ y. @y。
Based on my test, WPF App (.NET Framework) and WPF Application project does not need to add PresentationFramework.dll.根据我的测试,WPF App(.NET Framework)和WPF应用项目不需要添加PresentationFramework.dll。
Add PresentationFramework.dll to the Windows Forms App and Windows Forms App (.NET Framework) project: Add PresentationFramework.dll to the Windows Forms App and Windows Forms App (.NET Framework) project:
Right-click References (Dependencies) and select Add Reference... (Add COM Reference...)-> click Browse... ->find PresentationFramework.dll and click OK -> Confirm that PresentationFramework.dll dependency is checked and click ok. Right-click References (Dependencies) and select Add Reference... (Add COM Reference...)-> click Browse... ->find PresentationFramework.dll and click OK -> Confirm that PresentationFramework.dll dependency is checked and click好的。
Then you can use the following simple code to test.然后就可以使用下面的简单代码进行测试了。
using Microsoft.Win32;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
textBox1.Text = dlg.FileName;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.