【问题标题】:Auto-formatting lambda-functions in Visual studio 2010Visual Studio 2010 中的自动格式化 lambda 函数
【发布时间】:2012-01-03 16:26:46
【问题描述】:

如何设置 Visual Studio 2010 以使多行 lambda 函数看起来不难看,左侧有所有空白空间?

   dataView.CellFormatting += (s, e) =>
                                           {
                                               if ((e.ColumnIndex == 1)&&((dataView.SelectedCells.Count == 1)))
                                               {    
                                                   var scope = Scope.Instance;    
                                                   var row = dataView.Rows[e.RowIndex];
                                                   var variable = row.DataBoundItem as Variable;

                                                   if (scope.Variables.Contains(variable))
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                                                           scope.GetGraph(variable).Color;
                                                   }

                                                   else
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
                                                   }
                                               }
                                           };

【问题讨论】:

标签: c# visual-studio visual-studio-2010 lambda usability


【解决方案1】:

这取决于你认为丑陋的空白有多少,但你可以做的一件事是在相等后立即回车。然后你会得到这样的东西。 `

   {
        var raw_custs =
            (from customer in GetActive()
             where customer.Name.Contains(name)
             select customer).Take(numberToGet).ToList();

我通常在进行这样的更改后立即按 CTRl-E CTRL-D 以使文档自动格式化(编辑->高级->格式化文档)

(刚刚看到您修改后的帖子 - 当我将其放入 VS 并在 += 之后按回车时

dataView.CellFormatting +=
    (s, e) =>
    {
        if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
        {
            var scope = Scope.Instance;
            var row = dataView.Rows[e.RowIndex];
            var variable = row.DataBoundItem as Variable;

            if (scope.Variables.Contains(variable))
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                    scope.GetGraph(variable).Color;
            }

            else
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
            }
        }

【讨论】:

  • 谢谢,按回车键确实有帮助。
  • 不客气,你也可以在工具->选项->文本编辑器->C#->格式化->缩进下玩设置(哇!)我知道我有 缩进左大括号选项未选中。
【解决方案2】:

现在这很奇怪——缩进不应该那么远。

尝试将其剪切并粘贴到位,Visual Studio 应该会在粘贴时为您修复它。这是我得到的:

dataView.CellFormatting += (s, e) =>
{
    if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
    {
        var scope = Scope.Instance;
        var row = dataView.Rows[e.RowIndex];
        var variable = row.DataBoundItem as Variable;

        if (scope.Variables.Contains(variable))
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                scope.GetGraph(variable).Color;
        }

        else
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
        }
    }
};

【讨论】:

  • 是的,复制粘贴工作。但这不是很方便。
  • 我发现我必须按几次 Ctrl-KD(或 Ctrl-ED)才能“正确”完成 Lambda 表达式的格式化……它们似乎有问题……
猜你喜欢
  • 2019-03-13
  • 1970-01-01
  • 2011-11-04
  • 2013-04-18
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多