6

什麼是文檔依賴屬性的最佳方式?XML相關屬性文檔

我應該把賽場上的XML文檔:

/// <summary>Documentation goes here</summary> 
public static readonly DependencyProperty NameProperty = 
     DependencyProperty.Register(...) 

或對物業:

/// <summary>and/or here?</summary> 
public string Name{ get{...} set{...} } 

還是我真的需要記錄(和維護)兩者兼而有之?

+0

感謝您修復了錯字jeff :-) – Stefan 2010-10-29 16:54:04

回答

1

好吧,這是我想出的。

我在依賴項屬性上使用了一個特殊的xml標記,它將被一個xsl轉換所取代。沒有它就可以做到,但是Visual Studio會發出警告,因爲該字段看起來沒有記錄。

/// <dpdoc /> 
public static readonly DependencyProperty PositionProperty = 
    DependencyProperty.Register(...) 

C#屬性按照慣例記錄,只要確保不要忘記值描述。

/// <summary>Gets or sets the position of this element</summary> 
/// <value>Position (in pixel) relative to the parent's upper left corner.</value> 
/// <remarks><para> 
/// If either the <c>x</c> or <c>y</c> component is <c>+inf</c> this indicates... 
/// </para></remarks> 
public Point Position{ get{...} set{...} } 

Visual Studio在構建過程中根據這些註釋創建一個xml文件。通過一些xsl轉換,dpdoc節點被屬性文檔的修改版本所取代。生成的xml文件與我們很好地記錄屬性標識符相同。它甚至還包括一張紙條,上面有訪問變量的另一種方式:

/// <summary>Position (in pixel) relative to the parent's upper left corner.</summary> 
/// <remarks><para> 
/// If either the <c>x</c> or <c>y</c> component is <c>+inf</c> this indicates... 
/// <para> 
/// This dependency property can be accessed via the <see cref="Position"/> property. 
/// </para> 
/// </para></remarks> 
public static readonly DependencyProperty PositionProperty = 
    DependencyProperty.Register(...) 

這樣一來,既API的有適當的文件,我們也不需要複製的代碼的文檔。 xsl轉換可以在生成後事件中完成,也可以集成到文檔生成過程中。

這裏的XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="//dpdoc"> 
     <xsl:variable name="propertyName" select="concat('P:', substring(../@name,3,string-length(../@name)-10))" /> 
     <summary> 
      <xsl:apply-templates select="//member[@name=$propertyName]/value/node()"/> 
     </summary> 
     <xsl:apply-templates select="//member[@name=$propertyName]/*[not(self::remarks)][not(self::summary)][not(self::value)]"/> 
     <remarks> 
      <xsl:apply-templates select="//member[@name=$propertyName]/remarks/node()"/> 
      <para> 
       This dependency property can be accessed via the 
       <see> 
        <xsl:attribute name="cref"><xsl:value-of select="$propertyName"/></xsl:attribute> 
       </see> 
       property. 
      </para> 
     </remarks> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

爲什麼我想有這樣的說法:

  • 兩個屬性標識符(在DependencyProperty實例)和屬性,是公開的,因此可以合法地被用來訪問該財產。對於同一個邏輯變量,我們有兩個API。
  • 代碼文檔應該描述什麼不是已經存在的。在這種情況下,它應該描述財產的含義及其價值以及如何正確使用它。既然屬性標識符和c#屬性都引用相同的邏輯變量,則它們具有相同的含義。
  • 用戶可以自由選擇兩種方式之一來訪問邏輯變量,而不會意識到另一種。兩者都必須妥善記錄。
  • 複製粘貼代碼註釋與複製粘貼代碼一樣糟糕。
1

你應該記錄和維護兩者。一個是依賴項屬性,另一個是一個常規屬性,恰好實現爲訪問該依賴項屬性。他們不是一回事,需要單獨的文檔。

+1

好吧,當然有技術上的差異。但我不明白文檔將會如何變化。基本上都需要描述價值的含義。除此之外,它只是一個差異。 – Stefan 2010-10-29 16:53:21

+0

這是不正確的。實例屬性的實現有點不相關。呼叫者不應該依賴於實現。您應該從微軟那裏得到一個提示,他們很清楚地看到必須記錄這兩個項目。畢竟,一個人的用戶不需要知道他們必須查看另一個人的信息。假設兩者相互聯繫,很容易做出錯誤的假設。 – 2010-10-29 19:27:00

+1

微軟的財產領域文件非常差。它'識別[財產的名稱]依賴財產。'這是每個人都可以從它自己的聲明中第一眼看到的。這完全沒用(甚至可能是自動生成的)。它應該記錄財產的含義。另一點:財產的執行是相關的!首先,WPF假定該屬性直接由dp支持,因爲它繞過屬性並直接訪問dp。 – Stefan 2010-10-30 10:06:40