2017-09-13 21 views
2

私はlabelbuttonの2つのビューを動的に操作し、ボタンクリックにアクセスするにはボタンクリックにアクセスしようとしていますlistviewがあります。ボタンにアクセスする方法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(...')のコメントが1行にあることを願っていますか?それはコード化に大きな違いがあるので、ここに1行に入れてください。 – Kyra

答えて

3

をあなたはコンストラクタでViewCellにNavicationを渡すことができます。

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); 
    } 

そして、あなたは、単にあなたが結合してCommandParameterPropertyを使用することができます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()); 
    } 
} 

を私はこれはあなたのケースで動作するはずだと思うが、私は確認することはできません。あなたが投稿した内容はあなたのコードの1つではないと思います。私はseの初期化をあなたのコードに見ていないので、あなたのコード内のコメントは、質問。また、Homeクラスのコードは共有しません。そのときに何かが間違っている可能性があります。

+0

'' s'と 'e'の初期化は必要ありません。彼はラムダ式を使用しています:https://stackoverflow.com/questions/6531970/what-is-the-meaning-of-se-in-the-code – yiev

0

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

にナビゲーションオブジェクトにアクセスすることができます

例:

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

関連する問題