2017-09-13 80 views
2

我有listview有兩個視圖動態操作labelbutton,我試圖訪問按鈕點擊進入按鈕點擊的詳細信息頁面。下面如何訪問按鈕單擊Xamarin.Forms中的listview數據模板?

是我的自定義ViewCell

protected override async void OnAppearing() 
{ 
    listClass.ItemsSource = list; 
    listClass.ItemTemplate = new DataTemplate(typeof(ItemTemplateViewCell)); 
} 

public class ItemTemplateViewCell : ViewCell 
{ 

    Label NameLbl = new Label(); 
    StackLayout sLayout = new StackLayout(); 
    Button btnViewcell = new Button {Text = "Show class details"}; 
    public ItemTemplateViewCell() 
    { 
     NameLbl.SetBinding(Label.TextProperty, "Name"); 
     sLayout.Children.Add(NameLbl); 
     btnViewcell.Clicked += (s, e) => 
     { 
      // Navigation.PushAsync(new Home()); //I can not using this line 
      // does not exist in the current context, why cant i navigate to 
      // another page from inside datatemplate in List view 
     }; 
     sLayout.Children.Add(btnViewcell); 
     this.View = sLayout; 
    } 
} 
+0

我希望背後的註釋'Navigation.PushAsync(...'是在你的實際代碼中的一行?請把它放在一行,然後在這裏,因爲這確實在編碼上有很大的區別 – Kyra

回答

3

你可以通過構造函數傳遞Navication到ViewCell:

public class ItemTemplateViewCell : ViewCell 
    { 
     // Navigation Mermber 
     INavigation MyNavigation; 
     Label NameLbl = new Label(); 
     StackLayout sLayout = new StackLayout(); 
     Button btnViewcell = new Button {Text = "Show class details"}; 
     public ItemTemplateViewCell(INavigation navigation) 
     { 
      MyNavigation = navigation; 
      NameLbl.SetBinding(Label.TextProperty, "Name"); 
      sLayout.Children.Add(NameLbl); 
      btnViewcell.Clicked += ButtonShowDetails_Clicked; 
      sLayout.Children.Add(btnViewcell); 
      this.View = sLayout; 
     } 

     private void ButtonShowDetails_Clicked(object sender, EventArgs e) 
     {             
      MyNavigation.PushAsync(new Home()); 
     } 
    } 

然後通過委託功能通過Navication

protected override async void OnAppearing() 
    { 
     listClass.ItemsSource = list; 
     listClass.ItemTemplate = new DataTemplate(Function) ; 
    } 

    public object Function() 
    { 
     return new ItemTemplateViewCell (Navigation); 
    } 

然後,您可以訪問導航對象ViewCell

+0

我試過了,它的工作就像一個魅力,非常感謝Husam Zidan,代表非常棒 – Immortal

0

我的想法是怎麼了?是你在類的初始化立即執行動作。你應該像下面這樣分割它。另一個問題是你在方法之外進行變量的初始化。這可能會去好了,但我更喜歡下面的代碼:

public class ItemTemplateViewCell : ViewCell 
{ 

    Label nameLbl; 
    StackLayout sLayout; 
    Button btnViewcell; 
    public ItemTemplateViewCell() 
    { 
     nameLbl = new Label() 
     sLayout = new StackLayout() 
     btnViewcell = new Button {Text = "Show class details"}; 

     NameLbl.SetBinding(Label.TextProperty, "Name"); 
     sLayout.Children.Add(NameLbl); 
     btnViewcell.Clicked += OnButtonClicked; 
     sLayout.Children.Add(btnViewcell); 
     this.View = sLayout; 
    } 

    void OnButtonClicked(object sender, EventArgs e) 
    { 
     Navigation.PushAsync(new Home()); 
    } 
} 

我想這應該在你的情況下工作,但我不能肯定。我不認爲你發佈的是一對一的代碼,因爲我沒有在你的代碼中看到se的任何初始化,並且如評論中提到的那樣,如果你真的把它放在這個問題。你也不會分享Home類的代碼。這可能是錯誤的。

+0

他不需要's'和'e'的任何初始化。他使用的是Lambda表達式:https://stackoverflow.com/questions/6531970/what-is-the-meaning-of-se-in-the-code – yiev

0

Navigation不適用於ViewCell,這意味着您無法從您的ViewCell正確訪問它。但是,這應該工作:

App.Current.MainPage.Navigation.PushAsync(new Home()); 

整個代碼:

protected override async void OnAppearing() 
{ 
    listClass.ItemsSource = list; 
    listClass.ItemTemplate = new DataTemplate(typeof(ItemTemplateViewCell)); 
} 

public class ItemTemplateViewCell : ViewCell 
{ 

    Label NameLbl = new Label(); 
    StackLayout sLayout = new StackLayout(); 
    Button btnViewcell = new Button {Text = "Show class details"}; 
    public ItemTemplateViewCell() 
    { 
     NameLbl.SetBinding(Label.TextProperty, "Name"); 
     sLayout.Children.Add(NameLbl); 
     btnViewcell.Clicked += (s, e) => 
     { 
      App.Current.MainPage.Navigation.PushAsync(new Home()); 
     }; 
     sLayout.Children.Add(btnViewcell); 
     this.View = sLayout; 
    } 
} 
1

只要你能夠使用CommandParameterProperty與結合:

例子:

ListViewClass.ItemTemplate = new DataTemplate(() => 
{ 
    var GoHomeButton = new Button { Text = "Go Home", HorizontalOptions = LayoutOptions.StartAndExpand }; 
    GoHomeButton.SetBinding(Button.CommandParameterProperty, new Binding("Name")); 
    GoHomeButton.Clicked += Gohome; 
//return new view cell 
} 
    private void Gohome(object sender, EventArgs e) 
    {  
      Navigation.PushAsync(new Home()); 
    } 

欲瞭解更多信息查看以下鏈接:

http://www.c-sharpcorner.com/blogs/handling-child-control-event-in-listview-using-xamarinforms1

相關問題