【问题标题】:Resharper suggestion Page_Load = PageLoadResharper 建议 Page_Load = PageLoad
【发布时间】:2011-04-10 17:39:59
【问题描述】:

通常当 Page_Load 事件处理程序被 Visual Studio 添加到代码隐藏文件时(例如,在设计器模式下双击页面),它最终看起来像这样:

/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
    // ...
}

但 Resharper 建议使用 Name 'Page_Load' does not match rule 'Methods, properties and events'. Suggested name is 'PageLoad'。我猜有更好的方法来定义页面加载处理程序,但我不记得语法是什么,但我想它会解决这个 Resharper 警告?

可能是这样的:

/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    // ...
}

但我似乎记得OnLoadPageLoad 不完全相同?

【问题讨论】:

    标签: naming-conventions resharper pageload


    【解决方案1】:

    这是 ReSharper 6.0 及更低版本中的错误。在 6.1 中已修复。 对于 AutoEventWireup 处理程序,它不会建议任何更改,无论您有哪些命名设置。

    【讨论】:

      【解决方案2】:

      为了完整起见,我想我会在这里添加您从一位“大卫·本森”when you asked on jetbrains.net 那里得到的答案:

      如果您转到 ReSharper 选项,请在语言下选择 C# 命名样式。选择高级设置...按钮并在Event subscriptions on fields: 中,删除$event$ 前面的On,使其变为$object$_$event$。这应该会删除警告。

      对我来说,这是在现有的大型 Web 应用程序代码库面前安抚 ReSharper 的最简单方法。

      【讨论】:

      • 恕我直言,这是问题的正确答案(+1,因为它帮助了我)
      • 我同意。这是解决问题的一个更简单的方法。对我来说,这似乎是 R# 中的一个错误,默认情况下,如果您使用 Page_OnLoad,它不会发出警告。
      • 美女,这个问题困扰了我好几个月,我从来没有费心去查。这消除了 Page_Load Page_Init 等的警告,但保留了所有其他警告的警告。谢谢!
      【解决方案3】:

      OnLoadPage_Load 本质上是一样的。如果在 Page 上启用了 AutoEventWireup,则通过反射调用 Page_Load。 OnLoad 是 Control 类(Page 类继承)的受保护虚方法。

      请参阅Inside AutoEventWireup,深入分析 AutoEventWireup 的工作原理及其存在的原因。

      StackOverflow 社区也有一些inputonwhetheritisbetter 来处理Load 事件或覆盖OnLoad 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        相关资源
        最近更新 更多