簡體   English   中英

Resharper 將自動屬性轉換為完整屬性

[英]Resharper convert auto-property to full property

在這里發現了相反的情況,但我需要經常從自動屬性更改為完整屬性 - 是否可以自動執行(也許使用快捷方式):

來自汽車財產

public string FirstName { get; set; }

到具有支持字段的屬性

    private string _firstName;

    public string FirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
        }
    }

顯然,我會進一步改變整個財產......

將光標放在屬性名稱上,然后等待一兩秒鍾。 按Resharper熱鍵序列(Alt-Enter),第二個選項應為“To property with backing field”,這是您想要的。

或者,您可以單擊左邊距中的“錘子”圖標以獲取選項。

要使其工作(ALT-Enter),您必須配置resharper鍵盤架構。 VS - >工具 - >選項 - > ReSharper - >常規 - >選項 - >鍵盤和菜單 - > Resharper鍵盤架構 - > Visual Studio

我想添加一個擴展解決方案,它還允許以這種樣式創建一個屬性,它支持視圖模型的INotifyPropertyChanged

public string Name
{
    get => _name;
    set
    {
        if (value == _name) return;
        _name = value;
        RaisePropertyChanged();
    }
}

/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for the given <paramref name="propertyName"/>.
///
/// The <see cref="NotifyPropertyChangedInvocatorAttribute"/> attribute is used
/// because of Resharper support for "to property with change notification":
/// https://www.jetbrains.com/help/resharper/Coding_Assistance__INotifyPropertyChanged_Support.html
/// </summary>
/// <param name="propertyName"></param>
[NotifyPropertyChangedInvocator]
public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

所需的步驟是:

  • 在您的視圖模型所在的項目中安裝 Nuget package “JetBrains.Annotations”
  • 在引發PropertyChanged事件的方法上使用[NotifyPropertyChangedInvocator]屬性
  • 之后,“帶有更改通知的屬性”選項可用於您的視圖模型的屬性

在此處輸入圖像描述


另請參閱: Jetbrains INotifyPropertyChanged 支持

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM