Xamarin的.窗体控件布局单击侦听器



我在xamarin中使用网格视图。表单应用程序,我需要处理每个网格子事件。我怎么能知道哪一个被点击,而不是手动做它。提前感谢代码:

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

我认为你应该从Xamarin扩展Grid元素。以类似于这里描述的BoxView的方式形成,并为每个单元格添加自己的EventHandler。如果你要这样做,你还应该阅读有关CustomRenderers。

另一个选择是检查MR.Gestures,如果他们提供了你正在寻找的东西。

还有这个关于用Xamarin编写的自定义DataGrid的链接。表单也应该对你有价值。

最新更新