2011-09-29 6 views
0

* 모두 안녕하십니까, 저는 WP7 dev에 새로 도입되었습니다. (나는 안드로이드에서 일하는 데 익숙해 져있다.) 내가하는 법을 모르는 기본적인 것이있다.ApplicationBarIconButton에 매개 변수를 전달하십시오.

for (int i=0; i<menus.Count(); i++) 
{ 

    ApplicationBarIconButton button = new ApplicationBarIconButton(); 
    button.IconUri = new Uri(menus.ElementAt(i).Element("ImageUrl").Value.Trim(),UriKind.Relative); 
    button.Text = menus.ElementAt(i).Element("Title").Value.Trim(); 
    button.Click += new EventHandler(button_clicked); 
    ApplicationBar.Buttons.Add(button); 
} 

을하고 난 button_clicked 방법은 버튼의 I 값을 검색 할 수 있다고합니다 : 나는이 프로그래밍 방식 ApplicationBarIconButton의 목록을 만들 수 있습니다. 어떻게 가능합니까? 감사

답변

2

나는 @Enigmativity에 맞았지만 그의 대답은 여전히 ​​틀릴 수 있습니다. 내 경험에 비추어 볼 때 i 변수를 복제해야한다는 점을 발견했습니다. 그렇지 않으면 click 이벤트에서 i이 마지막 값이됩니다. 자신의 문제가 해결되지 않으면 것은

for (int i=0; i<menus.Count(); i++){ 
     ApplicationBarIconButton button = new ... 
     ... 
     var cloned = i; 
     button.Click += (sender, e) => { 
      sometTextBlock.Text = String.Format("App Button {0} pressed.", cloned); 
     }; 
    } 

건배, 알을 (다시 람 바어 기능 사용)을 시도합니다.

+0

저것에 저를 붙잡기를위한 감사합니다. 어리석은 느낌. : -/ – Enigmativity

+0

고맙습니다 ajmccall! – VinceFR

0

당신은이 작업을 수행 할 수 있습니다 :

for (int i=0; i<menus.Count(); i++) 
{ 
    ApplicationBarIconButton button = new ApplicationBarIconButton(); 
    button.IconUri = new Uri(menus.ElementAt(i).Element("ImageUrl").Value.Trim(),UriKind.Relative); 
    button.Text = menus.ElementAt(i).Element("Title").Value.Trim(); 
    var i2 = i; //Thanks to `ajmccall` - I forgot this. 
    button.Click += (s, e) => 
    { 
     // the variable `i2` is accessible now. 
    }; 
    ApplicationBar.Buttons.Add(button); 
} 

오히려 당신이 (로컬 복사본 i2를 통해) i에 접근 여전히 람다를 사용할 수 있습니다 클릭 처리하는 방법을 호출하는 것보다. 그런 다음 필요에 따라 i2을 매개 변수로 전달하는 메서드를 호출 할 수 있습니다.

0

이를 달성하는 통합 된 방법은 MVVM 프레임 워크에서 명령을 사용하는 것입니다. 응용 프로그램 바 버튼/메뉴 항목에 부여 된 것은 페이지에서 UI 요소를 조작하는 것보다 조금 까다 롭지 만 훨씬 유연합니다. MVVM 광에의

룩 (http://mvvmlight.codeplex.com) 또는 상기 Calburn.Micro의 좋아 (http://caliburnmicro.codeplex.com/)

바 애플리케이션 데이터를

바인딩 너는 google에 더를 필요로 할 것이다 (mo에 연결을 분실했다)

관련 문제