传递参数以点击手势Xamarin表格

时间:2017-09-25 13:30:04

标签: c# xamarin xamarin.forms

我正在尝试将参数从页面传递到另一个页面。这些传递的参数将用于从SQL表中进行选择。 该页面构建如下:(后面的代码)

private MainRoutePageViewModel mainroutepageviewmodel;
private List<RouteInfo> routeinfo;

构造

 public MainRoutePageViewDetail(MessagDatabase database)
    {
        InitializeComponent();

        BindingContext = mainroutepageviewmodel = new MainRoutePageViewModel(database,Navigation);
        //_listOfProperties = mainroutepageviewmodel.GetLabelInfo();

        ScrollView scrollview = new ScrollView();

        StackLayout mainstack = new StackLayout();
        mainstack.Spacing = 0;
        mainstack.Padding = 0;

        //mainstack.HeightRequest = 2000;

        routeinfo = mainroutepageviewmodel.GetLabelInfo();


        string _routePlacer = "";

        foreach (var i in routeinfo)
        {
            mainstack.Children.Add(NewRouteName(i.RouteName));


            mainstack.Children.Add(BuildNewRoute(i.RouteStops,i));


            _routePlacer = i.RouteName;
        }

        scrollview.Content = mainstack;

        Content = scrollview;


    }// end of constructor

BuildNewRoute方法:

public StackLayout BuildNewRoute(List<string> location, RouteInfo routeinfo)
    {

        StackLayout stackLayout = new StackLayout();

        //stackLayout.HeightRequest = 1000;

        foreach (var i in location) {

            StackLayout stackLayout2 = new StackLayout();
            stackLayout2.HeightRequest = 200;

            Grid grid = new Grid();
            grid.ColumnSpacing = 0;
            grid.RowSpacing = 0;



            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(15, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20, GridUnitType.Star) });

            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(55, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Star) });                
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(40, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(10, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(15, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Star) });


            TapGestureRecognizer ArrowtapGesture = new TapGestureRecognizer();
            ArrowtapGesture.Tapped += ArrowtapGesture_Tapped;

            // Arrow icon
            Image arrowimage = new Image();
            arrowimage.Source = "Resources/arrow.png";
            arrowimage.VerticalOptions = LayoutOptions.Center;
            arrowimage.HorizontalOptions = LayoutOptions.Center;
            arrowimage.GestureRecognizers.Add(ArrowtapGesture);
            grid.Children.Add(arrowimage,7,6);

            // total weight labels
            Label weightlabel = new Label();
            weightlabel.Text = "Total Weight [kg]: ";
            grid.Children.Add(weightlabel,1,5,3,4);

            // total items labels
            Label itemsLabel = new Label();
            itemsLabel.Text = "Total Items: ";
            grid.Children.Add(itemsLabel, 1, 5, 4, 5);

            // underline labels
            Label firstunderline = new Label();
            Label secondunderline = new Label();

            firstunderline.BackgroundColor = Color.Black;
            secondunderline.BackgroundColor = Color.Black;
            grid.Children.Add(firstunderline,0,9,0,1);
            grid.Children.Add(secondunderline,0,9,2,3);

            // address label
            Label labelLocation = new Label();
            labelLocation.Text = i;
            grid.Children.Add(labelLocation, 0, 3);

            //sequence label
            Label sequencelable = new Label();
            sequencelable.Text = "Sequence: ";
            sequencelable.VerticalTextAlignment = TextAlignment.Center;
            grid.Children.Add(sequencelable, 0, 1);

            // slot label
            Label slotlabel = new Label();
            slotlabel.Text = "ETA/Slot: ";
            slotlabel.VerticalTextAlignment = TextAlignment.Center;
            grid.Children.Add(slotlabel,1,4,1,2);

            // time label
            Label timelabel = new Label();
            timelabel.Text = "Time: ";
            timelabel.VerticalTextAlignment = TextAlignment.Center;
            grid.Children.Add(timelabel, 4, 5,1,2);

            // Status label
            Label statuslabel = new Label();
            statuslabel.Text = "Status: ";
            statuslabel.VerticalTextAlignment = TextAlignment.Center;
            grid.Children.Add(statuslabel, 5, 6,1,2);

            //start button
            Button startbutton = new Button();
            startbutton.Text = "Pending";
            startbutton.BackgroundColor = Color.Gray;
            grid.Children.Add(startbutton,5,8,4,6);

            // Phone book image
            Image bookImage = new Image();
            //bookImage.BackgroundColor = Color.White;
            bookImage.Source = "Resources/phoneWithBook.png";
            bookImage.VerticalOptions = LayoutOptions.Center;
            bookImage.HorizontalOptions = LayoutOptions.Center;
            grid.Children.Add(bookImage,1,2,6,7);


            //Globe image
            Image GlobeImage = new Image();
           // GlobeImage.BackgroundColor = Color.White;
            GlobeImage.Source = "Resources/globe.png";
            GlobeImage.VerticalOptions = LayoutOptions.Center;
            GlobeImage.HorizontalOptions = LayoutOptions.Center;
            grid.Children.Add(GlobeImage, 2, 3, 6, 7);


            stackLayout2.Children.Add(grid);

            stackLayout.Children.Add(stackLayout2);
        }

        return stackLayout;
    }

您可能会看到它循环遍历收集的数据列表,并将网格和标签添加到主StackLayout。 这不是页面构建工作正常的问题。

您可以看到的是带有水龙头手势的箭头图标图像。此点击手势使用视图模型打开下一页。

点按手势:

 private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
    {         

        await mainroutepageviewmodel.OpenStopDetail();
    }

OpenStopDetail方法:

 public async Task OpenStopDetail()
    {
        await Navigation.PushAsync(new StopDetailPageView());

    }

我想知道如何将参数从tap事件传递到StopDetailView页面。 特别是序列标签中的文字。

我尝试过的一些事情,一直在点击事件中使用转换,但这似乎与所选项目绑定。换句话说,它让我可以访问图像属性。这对我的情况没有好处。

我似乎找不到单独访问每个label属性以作为参数传递的方法。对不起,如果不清楚,很难解释。如果需要更多细节,请告诉我。

3 个答案:

答案 0 :(得分:3)

您应该可以使用TapGestureRecognizer的CommandParameter。

在XAML中: -

<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
                      CommandParameter="Value"/>

e.Parameter将是您在CommandParameter中设置的任何内容。

private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)

答案 1 :(得分:2)

Tapped事件的发件人将是手势识别器附加到的控件 - 在您的情况下是图像。因此,您可以将数据添加到Image的某个属性中,以便从事件处理程序中访问它。

// assign parameter to ClassId (must be a string)
arrowimage.ClassId = "blah";
arrowimage.GestureRecognizers.Add(ArrowtapGesture);

private async void ArrowtapGesture_Tapped(object sender, EventArgs e)
{         
    // retrieve parameter from sender's ClassId
    var parm = ((Image)sender).ClassId;
    await mainroutepageviewmodel.OpenStopDetail();
}

答案 2 :(得分:0)

   <Image.GestureRecognizers>
       <TapGestureRecognizer Tapped="Share_Tapped" CommandParameter="{Binding .}"/>
    </Image.GestureRecognizers>
    

enter code here
    private void Share_Tapped(object sender, TappedEventArgs e)
    {
        var contact = (e.Parameter) as DetailList;      


    }

结果

enter image description here