visual-studio Visual Studio 键盘快捷键可自动添加所需的“使用”语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/148977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:40:44  来源:igfitidea点击:

Visual Studio keyboard shortcut to automatically add the needed 'using' statement

visual-studiovisual-studio-2008keyboard-shortcuts

提问by Bramha Ghosh

What is the keyboard shortcut to expand the little red line that gives you the menu where you can choose to have the necessary usingstatement added to the top of the file?

什么是展开小红线的键盘快捷键,它为您提供菜单,您可以选择将必要的using语句添加到文件顶部?

回答by Orion Adrian

Ctrl+ .shows the menu. I find this easier to type than the alternative, Alt+ Shift+ F10.

Ctrl+.显示菜单。我觉得这更容易比其他输入,Alt+ Shift+ F10

This can be re-bound to something more familiar by going to Tools > Options > Environment > Keyboard > Visual C# > View.QuickActions

通过转到“工具”>“选项”>“环境”>“键盘”>“Visual C#”>“View.QuickActions”,可以将其重新绑定到更熟悉的内容

回答by Jeff Yates

Alt+ Shift+ F10will show the menu associated with the smart tag.

Alt+ Shift+F10将显示与智能标签关联的菜单。

回答by Pete

I can highly recommend checking out the Visual Studio plugin ReSharper. It has a QuickFix feature that does the same (and a lot more).

我强烈建议您查看 Visual Studio 插件 ReSharper。它具有执行相同(以及更多)的 QuickFix 功能。

But ReSharper doesn't require the cursor to be located on the actual code that requires a new namespace. Say, you copy/paste some code into the source file, and just a few clicks of Alt+ Enter, and all the required usings are included.

但是 ReSharper 不需要将光标定位在需要新命名空间的实际代码上。比如说,您将一些代码复制/粘贴到源文件中,只需点击几下Alt+ Enter,所有必需的用法都包括在内。

Oh, and it also makes sure that the required assembly reference is added to your project. Say for example, you create a new project containing NUnitunit tests. The first class you write, you add the [TestFixture] attribute. If you already have one project in your solution that references the NUnit DLL file, then ReSharper is able to see that the TestFixtureAttribute comes from that DLL file, so it will automatically add that assembly reference to your new project.

哦,它还确保将所需的程序集引用添加到您的项目中。例如,您创建了一个包含NUnit单元测试的新项目。您编写的第一个类,添加了 [TestFixture] 属性。如果您的解决方案中已有一个引用 NUnit DLL 文件的项目,那么 ReSharper 能够看到 TestFixtureAttribute 来自该 DLL 文件,因此它会自动将该程序集引用添加到您的新项目中。

And it also adds required namespaces for extension methods. At least the ReSharper version 5 beta does. I'm pretty sure that Visual Studio's built-in resolve function doesn't do that.

它还为扩展方法添加了所需的命名空间。至少 ReSharper 第 5 版测试版可以。我很确定 Visual Studio 的内置解析函数不会这样做。

On the down side, it's a commercial product, so you have to pay for it. But if you work with software commercially, the gained productivity (the plug in does a lot of other cool stuff) outweighs the price tag.

不利的一面是,它是一种商业产品,因此您必须为此付费。但是,如果您使用商业软件,获得的生产力(插件可以做很多其他很酷的事情)会超过价格标签。

Yes, I'm a fan ;)

是的,我是粉丝;)

回答by Gawie Kellerman

In Visual Studio 2010 you will find the keyboard command to resolve namespaces in a command called View.ShowSmartTag. Mine was also mapped to Shift+ Alt+ F10which is a lot of hassle - so I usually remap that promptly.

在 Visual Studio 2010 中,您将在名为View.ShowSmartTag的命令中找到用于解析命名空间的键盘命令。矿也映射到Shift+ Alt+F10是很多的麻烦-所以我通常重新映射及时。

On Pete commenting on ReSharper - yes, for anyone with the budget, ReSharper makes life an absolute pleasure. The fact that it is intelligent enough to resolve dependencies outside the current references, and add them both as usings and references will not only save you countless hours, but also make you forget where all framework classes reside ;-) That is how easy it makes development life... Then we have not even started on ReSharper refactorings yet.

关于皮特对 ReSharper 的评论 - 是的,对于任何有预算的人来说,ReSharper 让生活成为一种绝对的乐趣。事实上,它足够聪明地解决当前引用之外的依赖关系,并将它们添加为 using 和引用,不仅会为您节省无数时间,还会让您忘记所有框架类所在的位置 ;-) 这就是它的简单之处开发生命......然后我们甚至还没有开始 ReSharper 重构。

DevExpress' CodeRushoffers no assistance on this regard; or nothing that is obvious to me - and DevExpress under non-expert mode is quite forthcoming in what it wants to do for you :-)

DevExpress 的CodeRush不提供这方面的帮助;或者没有什么对我来说很明显 - 非专家模式下的 DevExpress 非常愿意为您做些什么:-)

Last comment - this IDE feature of resolving dependencies is so mature and refined in the Java IDE world that the bulk of the Internet samples don't even show the imports (using) any more.

最后评论 - 这个解决依赖关系的 IDE 特性在 Java IDE 世界中是如此成熟和完善,以至于大部分 Internet 示例甚至不再显示导入(使用)。

This said, Microsoft now finally has something to offer on this regard, but it is also clear to me that Microsoft development (for many of us) has now come full circle - the focus went from source, to visual designers right back to focus being on source again - meaning that the time you spend in a source code view / whether it is C#, VB or XAML is on the up and the amount of dragging and dropping onto 'forms' is on the down. With this basic assumption, it is simple to say that Microsoft should start concentrating on making the editor smarter, keyboard shortcuts easier, and code/error checking and evaluation better - the days of a dumb editor leaving you to google a class to find out in which library it resides are gone (or should be in any case) for most of us.

这就是说,微软现在终于在这方面提供了一些东西,但我也很清楚微软的开发(对我们中的许多人来说)现在已经完成了一个完整的循环——重点从源头到视觉设计师,又回到了重点再次在源代码上 - 这意味着您在源代码视图中花费的时间/无论是 C#、VB 还是 XAML 都在增加,而拖放到“表单”上的时间在减少。有了这个基本假设,很容易说微软应该开始专注于使编辑器更智能,键盘快捷键更容易,代码/错误检查和评估更好 - 一个愚蠢的编辑器的日子让你谷歌一个类来找出对于我们大多数人来说,它所在的库已经消失(或者无论如何应该消失)。

回答by James Curran

  • Context Menu key (one one with the menu on it, next to the right Windows key)
  • Then choose "Resolve" from the menu. That can be done by pressing "s".
  • 上下文菜单键(一个带有菜单的键,在右侧 Windows 键旁边)
  • 然后从菜单中选择“解决”。这可以通过按“s”来完成。

回答by Slobodan Stankovi?

It's ctrl+ .when, for example, you try to type Listyou need to type <at the end and press ctrl+ .for it to work.

它的ctrl+.时,例如,您尝试键入List您需要键入<在最后,并按ctrl+.为它工作。