简体   繁体   English

如何使用 C# 命名空间附带的 class 的方法

[英]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/

  • Tools->Nuget Package Manager->Manage Nuget Packages for Solutions...工具->Nuget Package 管理器->管理 Nuget 解决方案包...
  • Click Browse点击浏览
  • Search for PresentationFramework搜索 PresentationFramework
  • Install安装

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

The result is shown in the picture:结果如图所示: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中的方法中使用类名作为参数? - How do I use class names as parameters in methods in C#? C#-如何更改对象的类型,然后使用类中的方法? - C# - How do I change an Object's type and then use methods from the Class? 如何使用c#扩展方法扩展类? - How do I extend a class with c# extension methods? 我如何知道 C# Unity 中 AndroidJavaObject 类中的方法 - How do I know the Methods in AndroidJavaObject class in C# Unity C#如何将AOP用于静态方法? - C# How do I use AOP for static methods? 我可以导入一个 static class 作为命名空间来调用它的方法而不在 ZD7EFA19FBE7D3972FDZ5ADB6 中指定 class 名称吗? - Can I import a static class as a namespace to call its methods without specifying the class name in C#? 如何使其与 class 位于同一命名空间中的类可以访问变量,但命名空间之外的类不在 C# 中? - How do I make it so classes in the same namespace as a class have access to a variable but classes outside of a namespace don't in C#? C#/ .NET如何从类名中找到包含的命名空间 - C#/.NET How do I find the containing namespace from a class name C#列表-一旦数据在列表中,我是否再次使用类方法(获取/设置等)? - C# Lists - do I use Class Methods (Get/ Set etc) again once the data is in a list? 我如何在 C# 中使用方法? - How can i use methods in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM