2016-09-29 67 views
0

如何在其他单元格中添加按钮? 我试图做到这一点,但没有工作。感谢帮助。我的鳕鱼:其他单元格中的按钮:Xamarin.Forms

var button = new Image { Source = "page.png"}; 
     button.GestureRecognizers.Add(new TapGestureRecognizer 
     { 
      Command = new Command((async sender => 
      { 
       await Navigation.PushAsync(new page()); 
      })) 
     }); 

     var grid = new Grid(); 
     grid.ColumnDefinitions.Add(new ColumnDefinition { Width = 200 }); 

     grid.Children.Add(new Label { Text = "00"}, 0, 0); 
     grid.Children.Add(new Label { Text = "01"}, 0, 1); 
     grid.Children.Add(new Label { Text = "02"}, 0, 2); 

     grid.Children.Add(button, 1, 0); //this is not visible 
     grid.Children.Add(button, 1, 1); //this is not visible 
     grid.Children.Add(button, 1, 2); 

     grid.Children.Add(new Label { Text = "20"}, 2, 0); 
     grid.Children.Add(new Label { Text = "21"}, 2, 1); 
     grid.Children.Add(new Label { Text = "22"}, 2, 2); 
+0

您不能在多个单元中使用同一个控件实例。您需要为每个希望显示的单元格创建一个新实例。 – Jason

+0

如何做到这一点? – user5734658

回答

0

您不能在多个单元中使用同一个控件实例。您需要为每个想要它出现的单元格创建一个新实例。

var button1 = new Image { Source = "page.png"}; 
var button2 = new Image { Source = "page.png"}; 
var button3 = new Image { Source = "page.png"}; 

// add gesture recognizers to each instance 

grid.Children.Add(button1, 1, 0); 
grid.Children.Add(button2, 1, 1); 
grid.Children.Add(button3, 1, 2); 
相关问题