2012-04-30 7 views
3

Windows Phone 앱을 만들고 있는데 동적으로 생성 된 Button을 해당 이벤트 처리기와 연결할 수 없습니다.C# 코드로 만든 버튼의 클릭 이벤트를 메서드와 연관시키는 방법은 무엇입니까?

내가 사용하는 코드는 다음과 같습니다

public partial class MainPage : PhoneApplicationPage 
{ 
    Button AddButton 

    public MainPage() 
    { 
     CreateInterestEntry(); 
    } 

    private void CreateInterestEntry() 
    { 
     EntrySegment = getEntrySegment(); 
     ContentPanel.Children.Add(EntrySegment); 
    } 

    private void AddButton_Click(Object sender, RoutedEventArgs e) 
    { 
     //Stuff to do when button is clicked 
    } 

    private Grid getEntrySegment() 
    { 
     Grid EntrySegment = new Grid(); 

     //Creating the grid goes here 

     AddButton = new Button(); 
     AddButton.Content = "Add"; 
     AddButton.Click += new EventHandler(AddButton_Click); 

     return EntrySegment; 
    } 
} 

}는 AddButton_Click에 대한 과부하 "은 System.EventHandler을"위임과 일치하지 불평이 코드

. 내가 RoutedEventArgs에있는 EventArgs에서 AddButton_Click에 인수 유형을 변경 한을 제외하고

그것은 달리 비주얼 스튜디오는 암시은 System.EventHandler에서 시스템으로 변환 할 수 있음을 알려줍니다, MSDN에서 here 예 아래에있는 코드를 거의 동일합니다. RoutedEventHandler.

답변

2

이 시도 사전에

감사 :

AddButton.Click += new RoutedEventHandler(AddButton_Click); 

클릭 이벤트 처리기 :

void AddButton_Click(object sender, RoutedEventArgs e) 
{ 
} 
+0

감사합니다. 잘됐다. – user1364926

관련 문제