2011-03-13 2 views
1

이 특별한 경우/무의미한 overfluid 것 같다 디스패처에 대한 대리자에 할당하기 위해 추가 SetUIText 메서드를 만들고 싶습니다.C# => 람다 표현식이 내가 지금 무엇을 가지고

public void SetText(FlowDocument document, string text) 
{ 
    Action<FlowDocument, string> action; 

    action = (doc, txt) => 
    { 
     TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); 
     using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) 
     { 
      tr.Load(ms, DataFormats.Rtf); 
     } 
    }; 

    Dispatcher.CurrentDispatcher.BeginInvoke(action,DispatcherPriority.Background, document, text); 
} 

그냥 람다의 매개 변수이 txt의 문서를 확인하십시오

은 그래서 람다 식을 도입했다. 두 매개 변수는 사용되지 않습니다. 내가 대신 사용하는 것은 람다 식 내부의 문서와 텍스트입니다.

doc OR 문서 및 txt OR 텍스트를 사용할 수 있습니다. 이 람다 표현식을 사용할 가치가 있습니까? 처음 두 가지 방법을 고수해야합니까?

+4

왜 : 당신은 심지어 빈 매개 변수 목록을 작성 피할 수도, 자연 보일 수 있습니다 익명 메소드와 같은 작업을 수행 할 수 있습니다

public void SetText(FlowDocument document, string text) { Action action =() => { TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) { tr.Load(ms, DataFormats.Rtf); } }; Dispatcher.CurrentDispatcher.BeginInvoke(action, DispatcherPriority.Background); } 

참고 :이를 사용합니다 당신은 당신의 lamda에 매개 변수를 전혀 사용합니까? 로컬'document'와'text' 변수를 잡을 수 있습니다. – CodesInChaos

답변

5

나는 CodeInChaos의 의견에 동의합니다. 왜 두 가지 매개 변수를 취하는 행동으로 귀찮게합니까?

Action action = delegate 
{ 
    TextRange tr = new TextRange(document.ContentStart, 
              document.ContentEnd); 
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) 
    { 
     tr.Load(ms, DataFormats.Rtf); 
    } 
}; 
+0

예 존 저는 방금 매개 변수없이() => 그 overfluid LOL을 깨달았습니다 ... 그리고 또 다른 감사 Jon the Action 액션 = 델리게이트는 제가 찾고있는 것입니다! 익명 방법 ... :) – msfanboy

관련 문제