2011-12-13 2 views
1

C#에서 데이터 바인딩을 사용하여 winforms 응용 프로그램을 작성했습니다. 나는 최근에 아래에 표시된 코드와 같은 많은 속성을 작성하고 있음을 알게되었습니다. 그 코드를 작성하는 것은 괜찮지 만 무언가를 놓친다 고 생각합니다. 코드를 적게 만들고 코드를보다 깨끗하게 만들 수 있습니까?내 클래스의 데이터 바인딩 준비 속성을 자동으로 생성하는 방법이 있습니까?

이렇게하는 자동화 된 방법이 있습니까?
Codedom 또는 모든 프레임 워크와 비슷합니까?

public class SampleClass : INotifyPropertyChanged 
{ 
    public Boolean Enabled 
    { 
     get { return _enabled; } 
     set 
     { 
      if (_enabled == value) return; 

      _enabled = value; 

      // broadcast the change 
      RaisePropertyChanged(PropertyName_Enabled); 

      // this object is modified 
      this.Modified = true; 
     } 
    } 

    public Single Degree 
    { 
     get { return _degree; } 
     set 
     { 
      if (_degree == value) return; 

      _degree = value; 

      // broadcast the change 
      RaisePropertyChanged(PropertyName_Degree); 

      // this object is modified 
      this.Modified = true; 
     } 
    } 

    // Define the property name this class exposes and notifies 
    public static readonly String PropertyName_Enabled = "Enabled"; 
    public static readonly String PropertyName_Degree = "Degree"; 

    private Boolean _enabled; 
    private Single _degree;  
}  

답변

1

그냥 만들 code snippet :

<?xml version="1.0" encoding="utf-8"?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 
    <CodeSnippet Format="1.0.0"> 
    <Header> 
     <SnippetTypes> 
     <SnippetType>Expansion</SnippetType> 
     </SnippetTypes> 
     <Title>propnot</Title> 
     <Author>Thomas Levesque</Author> 
     <Description>Code snippet for property with change notification</Description> 
     <HelpUrl> 
     </HelpUrl> 
     <Shortcut>propnot</Shortcut> 
    </Header> 
    <Snippet> 
     <Declarations> 
     <Literal Editable="true"> 
      <ID>type</ID> 
      <ToolTip>Property type</ToolTip> 
      <Default>int</Default> 
      <Function> 
      </Function> 
     </Literal> 
     <Literal Editable="true"> 
      <ID>property</ID> 
      <ToolTip>Property name</ToolTip> 
      <Default>MyProperty</Default> 
      <Function> 
      </Function> 
     </Literal> 
     <Literal Editable="true"> 
      <ID>field</ID> 
      <ToolTip>The variable backing this property</ToolTip> 
      <Default>myProperty</Default> 
      <Function> 
      </Function> 
     </Literal> 
     <Literal Editable="true"> 
      <ID>notifyMethod</ID> 
      <ToolTip>The method used to notify the listeners</ToolTip> 
      <Default>OnPropertyChanged</Default> 
      <Function> 
      </Function> 
     </Literal> 
     </Declarations> 
     <Code Language="csharp"><![CDATA[private $type$ $field$; 
public $type$ $property$ 
{ 
    get { return $field$;} 
    set 
    { 
     $field$ = value; 
     $notifyMethod$("$property$"); 
    } 
} 
$end$]]></Code> 
    </Snippet> 
    </CodeSnippet> 
</CodeSnippets> 

(이 정확하게 표시된 코드와 동일하지 않습니다,하지만 당신은 쉽게 당신의 정확한 요구에 적응할 수)

틀림를의 코드는 더 깔끔하게 보일 수는 없지만 적어도 쓰는 데는 더 적은 시간을 소비하게됩니다.)

관련 문제