【问题标题】:WPF Code-Behind required namespacesWPF 代码隐藏所需的命名空间
【发布时间】:2014-10-27 05:48:03
【问题描述】:

我想保持我的代码整洁有序。
创建.xaml 文件(例如,典型 WPF MVVM 模式的视图)时,Visual Studio 会自动在代码隐藏中为您的视图添加一些命名空间。我想删除未使用的冗余名称空间。我的问题是:
哪些命名空间要保存以删除,哪些不是底层 WPF 机制所必需的?

例如,System.Window 命名空间显然是必需的,因为 Code-Behind 类继承了 Window 类。但是其余的哪一个可以安全地在像这样的空白代码隐藏文件中删除:

当图像在未来不工作时的参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DatabaseProject.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindowView : Window
    {
        public MainWindowView()
        {
            InitializeComponent();
        }
    }
}

【问题讨论】:

  • 我相信您只需要System.Windows。把它们一个一个拿出来,看看有没有坏东西。也必须大声喊出Reshaper。它将使用 alt + enter 为您删除未使用的那些。不过不是免费的。
  • Visual Studio 有Organize Usings 选项。
  • 使用 IntelliSense Remove Unused Usings 时,在这种情况下仅保留System.Windows。我猜在使用 XAML 添加控件和绑定时不需要控件和数据?
  • 一个词:Resharper! 它为你清理了这个烂摊子 (ctrl-e-c) :) 我不能没有它!对我来说,没有 R# 就没有 vs ,除非我正在编写 c++ ;) 试试吧,你将终生着迷!

标签: c# wpf visual-studio namespaces code-behind


【解决方案1】:

全部删除,然后VS会在无法识别的单词下划线,然后使用Ctrl + '.'每个要导入。当鼠标光标悬停在红色标记的文本上时,Resharper 将自动建议丢失的命名空间。然后你需要点击它,所需的命名空间将立即添加。

【讨论】:

  • 为了简单起见,我更喜欢 Jesse Good 的评论中建议的 Organize Usings
  • +1 R# :) 完全清理也将解决上述问题。我怀疑用户没有它,因为那样就不会有问题了:)
【解决方案2】:

Visual Studio Productivity Power Tools 具有在保存文件时删除未使用的选项(默认禁用)以及删除和排序整个项目的使用的选项。

正如你所说的你喜欢干净有组织的代码,你可能也喜欢保存选项中的格式文档。

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多