2015-09-20 102 views
0

我在我的xamarin.forms應用程序中使用網格視圖,其中我需要處理每個網格子項的事件。我怎麼知道哪一個被點擊而不是手動執行。在此先感謝 代碼:Xamarin.forms grib佈局點擊監聽器

namespace eSewaXamarin 
{ 

    public class QuickMenu:ContentPage 
    { 

     public QuickMenu(){ 


      this.Title="QuickMenu"; 
      this.BackgroundColor =Colors.lightGrey ; 
      int rowHeight = 100; 

      Grid grid = new Grid { 
       BackgroundColor = Color.FromHex("E0E0E0"), 
       Padding = new Thickness(10, 0, 10, 0), 
       RowSpacing=5, 
       ColumnSpacing=5, 
       RowDefinitions={ 
        new RowDefinition{Height=rowHeight}, 
        new RowDefinition{Height=115}, 
        new RowDefinition{Height=rowHeight}, 
        new RowDefinition{Height=115} 
       }, 
       ColumnDefinitions = { 
        new ColumnDefinition {}, 
        new ColumnDefinition {} 
       } 

      }; 
      var contentView00=new ContentView(); 
      contentView00 = getContentView (0, 0); 
      grid.Children.Add(contentView00, 0,0); 

      var contentView10=new ContentView(); 
      contentView10 = getContentView (1, 0); 
      grid.Children.Add(contentView10, 1,0); 

      var contentView01=new ContentView(); 
      contentView01 = getContentView (0, 1); 
      grid.Children.Add(contentView01, 0,1); 

      var contentView11=new ContentView(); 
      contentView11 = getContentView (1, 1); 
      grid.Children.Add(contentView11, 1,1); 

      var contentView02=new ContentView(); 
      contentView02 = getContentView (0, 2); 
      grid.Children.Add(contentView02, 0,2); 

      var contentView12=new ContentView(); 
      contentView12 = getContentView (1, 2); 
      grid.Children.Add(contentView12, 1,2); 

      var contentView03=new ContentView(); 
      contentView03 = getContentView (0, 3); 
      grid.Children.Add(contentView03, 0,3); 

      var contentView13=new ContentView(); 
      contentView13 = getContentView (1, 3); 
      grid.Children.Add(contentView13, 1,3); 

      // Accomodate iPhone status bar. 
      this.Padding = new Thickness(0, Device.OnPlatform(30, 10, 10), 0, 0); 

      var fonePayIcon = new Image{ 
       Aspect = Aspect.AspectFit, 


      }; 
      fonePayIcon.Source = ImageSource.FromFile ("footer_logo.png"); 


      // Build the page. 
      this.Content = new ScrollView { 
       Content = new StackLayout{ 
        Children={grid,new StackLayout{ 
          Padding=new Thickness(0,8,0,8), 
          BackgroundColor=Color.FromHex ("dfdfdf"), 
          VerticalOptions=LayoutOptions.EndAndExpand, 
          Children={fonePayIcon} 
         } 
         } 
       } 
      }; 


     // this.ToolbarItems.Add (new ToolbarItem() { Icon = "icinformations.png", Command = new Command(()=> App.GoTo("info")) }); 

      var tgr = new TapGestureRecognizer { NumberOfTapsRequired = 1 }; 
      tgr.Tapped += OnTapGestureRecognizerTapped; 
      contentView00.GestureRecognizers.Add(tgr); 

     } 
     void OnTapGestureRecognizerTapped(object sender, EventArgs args) 
     { 
      //AppLog.showlog ("Args:::::: "+ args.); 
      Navigation.PushModalAsync (new NtcPrepaid()); 
     } 

     ContentView getContentView(int r,int c){ 
      var serviceIcon = new Image{ Aspect = Aspect.AspectFit}; 
      String serviceName=""; 
      if (r == 0 && c == 0) { 
       serviceIcon.Source = ImageSource.FromFile ("ntc.png"); 
       serviceName = Strings.quickMenulabel00; 
      } else if (r == 1 && c == 0) { 
       serviceIcon.Source = ImageSource.FromFile ("recharge_cards.png"); 
       serviceName = Strings.quickMenulabel10; 
      }else if (r == 0 && c == 1) { 
       serviceIcon.Source = ImageSource.FromFile ("ncellone.png"); 
       serviceName = Strings.quickMenulabel01; 
      }else if (r == 1 && c == 1) { 
       serviceIcon.Source = ImageSource.FromFile ("ntc.png"); 
       serviceName = Strings.quickMenulabel11; 
      }else if (r == 0 && c == 2) { 
       serviceIcon.Source = ImageSource.FromFile ("adsl.png"); 
       serviceName = Strings.quickMenulabel02; 
      }else if (r == 1 && c ==2) { 
       serviceIcon.Source = ImageSource.FromFile ("dishhome.png"); 
       serviceName = Strings.quickMenulabel12; 
      }else if (r == 0 && c == 3) { 
       serviceIcon.Source = ImageSource.FromFile ("sim_tv.png"); 
       serviceName = Strings.quickMenulabel03; 
      }else if (r == 1 && c == 3) { 
       serviceIcon.Source = ImageSource.FromFile ("esewa_transfer.png"); 
       serviceName = Strings.quickMenulabel13; 
      } 
      var label = new Label { 
       TextColor=Colors.black, 
       Text = serviceName, 
       XAlign = TextAlignment.Center 
      }; 

      var contentView=new ContentView{ 
       BackgroundColor=Color.White, 
       Content=new StackLayout{ 
        Children={new StackLayout{ 
          Padding=new Thickness(0,20,0,0), 
          Children={serviceIcon}},label} 
       } 
      }; 
      return contentView; 

     } 

    } 
} 

回答

0

我想你應該在類似的方式就像是描述hereBoxView從Xamarin.Forms延長Grid元素,並添加自己的EventHandler每個細胞。如果你這樣做,你還應該閱讀關於CustomRenderers

另一種選擇是檢查MR.Gestures,如果他們提供您正在尋找的東西。

此鏈接關於自定義DataGrid寫在Xamarin.Forms也應該爲你值得。