Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xamarin表单-是否可以获取网格行选择事件?_Xamarin_Grid_Xamarin.forms - Fatal编程技术网

xamarin表单-是否可以获取网格行选择事件?

xamarin表单-是否可以获取网格行选择事件?,xamarin,grid,xamarin.forms,Xamarin,Grid,Xamarin.forms,如何获取网格行选择的行id?我想添加行唯一id,我想点击或点击它。如果有人有任何想法,请告诉我 我想做一些类似的事情,如果我点击该行并获得id。使用此id,我将获取另一条记录并在另一个页面上发送给用户。为此,我需要行的唯一id 我有网格状的吼声: 下面是我使用的代码: public BusList(Common busdata) { InitializeComponent(); this.BindingContext = this;

如何获取网格行选择的行id?我想添加行唯一id,我想点击或点击它。如果有人有任何想法,请告诉我

我想做一些类似的事情,如果我点击该行并获得id。使用此id,我将获取另一条记录并在另一个页面上发送给用户。为此,我需要行的唯一id

我有网格状的吼声:

下面是我使用的代码:

    public BusList(Common busdata)
    {
        InitializeComponent();
        this.BindingContext = this;
        List<BusDetail> Bus = new List<BusDetail>
        {
            ...
            new BusDetail("Nita Travel","Mumbai", "Navsari",new DateTime(2017, 3, 9), "10:15 AM", "09:15 AM")
        };

        Label header = new Label
        {
            Text = "GridView Demo",
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            HorizontalOptions = LayoutOptions.Center
        };


        var controlGrid = new Grid
        {

            RowSpacing = 2,
            ColumnSpacing = 2,
            Margin = new Thickness(5, Device.OnPlatform(5, 0, 0), 5, 5)
        };
        controlGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
        for (int i = 0; i < Bus.Count(); i++)
        {
            controlGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
        }
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
       controlGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });



        Bus = Bus.Where(x => x.FromDest.ToLower().Contains(busdata.FromDest.ToLower()) && x.FromDate == busdata.FromDate).ToList();
        controlGrid.Children.Add(new Label { Text = "ID", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 0, 0);
        controlGrid.Children.Add(new Label { Text = "Travel Name", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 1, 0);
        controlGrid.Children.Add(new Label { Text = "Arr Time", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 2, 0);
        controlGrid.Children.Add(new Label { Text = "Dep Time", Font = Font.SystemFontOfSize(NamedSize.Large).WithAttributes(FontAttributes.Bold), BackgroundColor = Color.Gray }, 3, 0);


        for (int i = 0; i < Bus.Count(); i++)
        {
            var clickableRow = new ContentView();

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "RowTappedCommand");
            tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, i.ToString()); 

            clickableRow.GestureRecognizers.Add(tapGestureRecognizer);
            clickableRow.BindingContext = i.ToString();
            controlGrid.Children.Add(clickableRow, 0, i);
            Grid.SetColumnSpan(clickableRow, 3);


            controlGrid.Children.Add(new Label { Text = Bus[i].TravelName, BindingContext = Bus[i].TravelName, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 1, i + 1);
            controlGrid.Children.Add(new Label { Text = Bus[i].ArrivalTime, BindingContext = Bus[i].ArrivalTime, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 2, i + 1);
            controlGrid.Children.Add(new Label { Text = Bus[i].DepartureTime, BindingContext= Bus[i].DepartureTime, BackgroundColor = Color.LightGray, VerticalTextAlignment = TextAlignment.Center }, 3, i + 1);

        }
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
        this.Content = new StackLayout
        {
            Children =
            {
                header,
                controlGrid
            }
        };
    }

  public  void RowTappedCommand(string index)
    {
        var tappedRow = int.Parse(index);

    }
公共总线列表(公共总线数据)
{
初始化组件();
this.BindingContext=this;
列表总线=新列表
{
...
新巴士详情(“尼塔旅行”、“孟买”、“纳瓦萨里”、新日期时间(2017年3月9日)、“上午10:15”、“上午9:15”)
};
标签标题=新标签
{
Text=“GridView演示”,
FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Label)),
水平选项=布局选项。居中
};
var controlGrid=新电网
{
行间距=2,
列间距=2,
余量=新厚度(5,设备平台上(5,0,0),5,5)
};
添加(新行定义{Height=newgridlength(30)});
对于(int i=0;ix.FromDest.ToLower().Contains(busdata.FromDest.ToLower())和&x.FromDate==busdata.FromDate.ToList();
controlGrid.Children.Add(新标签{Text=“ID”,Font=Font.SystemFontOfSize(NamedSize.Large)。WithAttributes(FontAttributes.Bold),BackgroundColor=Color.Gray},0,0);
controlGrid.Children.Add(新标签{Text=“Travel Name”,Font=Font.SystemFontOfSize(NamedSize.Large)。WithAttributes(FontAttributes.Bold),BackgroundColor=Color.Gray},1,0);
controlGrid.Children.Add(新标签{Text=“Arr Time”,Font=Font.SystemFontOfSize(NamedSize.Large)。WithAttributes(FontAttributes.Bold),BackgroundColor=Color.Gray},2,0);
controlGrid.Children.Add(新标签{Text=“Dep Time”,Font=Font.SystemFontOfSize(NamedSize.Large)。WithAttributes(FontAttributes.Bold),BackgroundColor=Color.Gray},3,0);
对于(int i=0;i
您可以使用
ColumnSpan
属性将
ContentView
添加到第一列的每一行,并跨越所有列。然后,您可以使用
TouchGestureListener
来处理点击。创建
命令
时,还需要包含一个
CommandParameter
,其中包含相关行的索引

在构造函数之外,定义以下内容:

public ICommand RowTappedCommand { get; private set; }
然后在InitializeComponent之后的某个位置添加以下代码:

RowTappedCommand = new Command<string>(RowTapped);
您还需要定义与
CommandProperty
中定义的名称匹配的方法。如果为视图定义了视图模型,则需要在那里执行此操作。否则,您需要将该方法添加到视图的代码隐藏文件(xaml.cs)中

请记住正确设置BindingContext,以便调用您的命令。以下是有关Xamarin.Forms中命令的更多信息:


更新:有一些错误需要修复

  • 命令定义不正确
  • 命令属性的绑定需要指向静态值

  • 谢谢@hankide的回复,我还有一个问题是如何使用它?我有什么办法吗??很抱歉我的问题,但我是xamarin的新手,所以请帮助我。我在.cs文件中添加了此代码,但它不起作用。我在RowTappedCommand方法中没有收到调用。请帮我解决这个问题。@Dhruti是否设置了绑定上下文?@Dhruti如果您的所有代码都在代码隐藏文件中,请在初始化组件后将其放入构造函数:
    this.BindingContext=this
    。这样就可以找到命令
    var clickableRow = new ContentView {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.FillAndExpand
    }
    
    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "RowTappedCommand");
    var binding = new Binding();
    binding.Source = i.ToString();
    tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, binding); // This is the index of the row
    clickableRow.GestureRecognizers.Add(tapGestureRecognizer);
    
    controlGrid.Children.Add(clickableRow, 0, 0);
    Grid.SetColumnSpan(clickableRow,3);
    
    void RowTapped(string index)
    {
        var tappedRow = int.Parse(index);
        // Do something with the value
    }