Я не могу вызвать команду при нажатии на элементы списка просмотра Xamarin формы - PullRequest
1 голос
/ 20 января 2020

Я столкнулся с проблемой, при которой в представлении List я могу вызвать первый элемент и только первый. Я также хочу вызвать мою команду, когда строка Cell щелкается где-либо внутри, но моя команда срабатывает только тогда, когда щелкает метка, и только в первой ячейке. Любые идеи, чтобы решить мою проблему? Большое спасибо

<StackLayout Grid.Row="3">
  <ListView x:Name="meetingList" ItemsSource="{Binding MeetingDetails}" RowHeight = "100">
    <ListView.ItemTemplate >
      <DataTemplate>
        <ViewCell>
          <StackLayout BackgroundColor="Red">
            <ContentView>
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="40"/>
                  <RowDefinition Height="40"/>
                  <RowDefinition Height="40"/>
                  <RowDefinition Height="40"/>
                </Grid.RowDefinitions>
                <StackLayout  Margin="10,0" BackgroundColor="red" Padding="15,10,15,10" HeightRequest="100" Orientation="Vertical" HorizontalOptions="StartAndExpand" >
                  <Label x:Name="Label_Name" Text="{Binding Name}" />
                  <Label>
                    <Label.FormattedText>
                      <FormattedString>
                        <Span Text="{Binding DateOfStart}" FontAttributes="Bold"/>
                        <Span Text="-"></Span>
                        <Span Text="{Binding DateOfEnd}" FontAttributes="Bold" />
                      </FormattedString>
                    </Label.FormattedText>
                    <Label.GestureRecognizers>
                      <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=calendarPage}}"></TapGestureRecognizer>
                    </Label.GestureRecognizers>
                  </Label>
                </StackLayout>
              </Grid>
            </ContentView>
            <StackLayout.GestureRecognizers>
              <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=meetingList}}"></TapGestureRecognizer>
            </StackLayout.GestureRecognizers>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

введите описание изображения здесь

Ответы [ 2 ]

1 голос
/ 21 января 2020

Я изменил ваш макет, как показано ниже.

    <StackLayout Grid.Row="3">
    <ListView x:Name="meetingList" ItemsSource="{Binding MeetingDetails}" RowHeight = "100">
        <ListView.ItemTemplate >
            <DataTemplate>
                <ViewCell>
                    <StackLayout BackgroundColor="Red">

                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=meetingList}}" CommandParameter="{Binding .}"></TapGestureRecognizer>
                        </StackLayout.GestureRecognizers>

                        <ContentView>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="40"/>
                                    <RowDefinition Height="40"/>
                                    <RowDefinition Height="40"/>
                                    <RowDefinition Height="40"/>
                                </Grid.RowDefinitions>
                                <StackLayout  Margin="10,0" BackgroundColor="red" Padding="15,10,15,10" HeightRequest="100" Orientation="Vertical" HorizontalOptions="StartAndExpand" >
                                    <Label x:Name="Label_Name" Text="{Binding Name}" />
                                    <Label>
                                        <Label.FormattedText>
                                            <FormattedString>
                                                <Span Text="{Binding DateOfStart}" FontAttributes="Bold"/>
                                                <Span Text="-"></Span>
                                                <Span Text="{Binding DateOfEnd}" FontAttributes="Bold" />
                                            </FormattedString>
                                        </Label.FormattedText>
                                        <Label.GestureRecognizers>
                                            <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand}"></TapGestureRecognizer>
                                        </Label.GestureRecognizers>
                                    </Label>
                                </StackLayout>
                            </Grid>
                        </ContentView>

                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

Здесь работает GIF. enter image description here

Я не знаю, что вы имеете в виду о <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=calendarPage}}"></TapGestureRecognizer>

Если вы хотите получить модель элемента клика. Вы можете использовать CommandParameter как этот код. <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=meetingList}}" CommandParameter="{Binding .}"></TapGestureRecognizer>

В ViewModel вы можете получить модель щелчка.

  ShowDetailsCommand = new Command<MyModel>((key)=> {

            var myStr = key;

            Console.WriteLine("Name: " + myStr.Name+" "+myStr.DateOfStart+" "+myStr.DateOfEnd);

        });

Вот мое демо. https://github.com/851265601/ListviewCLickCommand

1 голос
/ 20 января 2020

Проблема в том, что:

<StackLayout.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding BindingContext.ShowDetailsCommand, Source={x:Reference Name=meetingList}}"></TapGestureRecognizer>
</StackLayout.GestureRecognizers>

Захватывает Tap, и поэтому внутренний TapGestureRecognizer ничего не делает, потому что Tap уже захвачен. Лучший способ - использовать EventToCommandBehavior

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/reusable/event-to-command-behavior

и захватить событие ItemSelected. Что-то в этом духе думаю, что мне нужно использовать TapGestureRecognizer, то я делаю что-то не так. Но это, вероятно, мнение

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...