从Xamarin来自

时间:2018-06-18 14:53:09

标签: c# xaml xamarin.forms

我正在向CommandParameter发送TapGestureRecognizer,并且我的OnTapGestureRecognizerTapped功能的对象参数中提供了<ListView x:Name="listViewEvents" RowHeight="120" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Grid Padding="5"> <Grid.RowDefinitions> <RowDefinition Height="40"></RowDefinition> <RowDefinition Height="60"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="90"></ColumnDefinition> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" FontSize="Large" Grid.ColumnSpan="2" Text="{Binding UploadTitle}" LineBreakMode="TailTruncation"></Label> <Label Grid.Row="1" Grid.Column="0" Text="{Binding UploadDescription}" Font="Small" TextColor="Gray" LineBreakMode="TailTruncation"></Label> <Image Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Source="{Binding AttachPath}" Aspect="AspectFill"> <Image.GestureRecognizers> <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped" NumberOfTapsRequired="2" CommandParameter="{Binding AttachPath_New}" /> </Image.GestureRecognizers> </Image> <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding FileName}" LineBreakMode="TailTruncation"></Label> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> 。但我无法从中汲取价值。我该如何解决这个问题?

我的列表视图(XAML标记):

void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
        string fullPath = "";
        Image imageSender = (Image)sender;

        if (imageSender.GestureRecognizers.Count > 0)
        {
            fullPath = imageSender.GestureRecognizers[0].ToString();
        }
        DownloadFile(imageSender.Source.GetValue(UriImageSource.UriProperty).ToString());
}

我的C#页面:

<% if controller.action_name.to_s == "index" %>

我在这里得到了我的价值:

Enter image description here

2 个答案:

答案 0 :(得分:2)

您正在尝试将整个GestureRecognizer转换为字符串,而不仅仅是其CommandParameter属性。

    void OnTapGestureRecognizerTapped(object sender, EventArgs args)
    {
        string fullPath = "";
        Image imageSender = (Image)sender;
        if (imageSender.GestureRecognizers.Count > 0)
        {
            var gesture = (TapGestureRecognizer)imageSender.GestureRecognizers[0];
            fullPath = (string)gesture.CommandParameter;
        }
        DownloadFile(imageSender.Source.GetValue(UriImageSource.UriProperty).ToString());
    }

答案 1 :(得分:0)

您可以使用的另一种解决方案是将 EventArgs 类型转换为 TappedEventArgs 并公开参数。 因此,要访问 CommandParameter,您将编写以下内容:

private void OnTapped(object sender, EventArgs e)
{
var te = (TappedEventArgs)e;
string parameter = (string)te.Parameter;
....