Xamarin.Forms GestureRecognizers在Command上不起作用

问题描述 投票:0回答:2

xaml页面中的代码:

  <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">        
            <CollectionView ItemsSource="{Binding MeniElementi}">
                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical"
                        Span="2" />
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Frame Padding="10" WidthRequest="140" HeightRequest="140">
                            <Frame BackgroundColor="AliceBlue" WidthRequest="120" HeightRequest="120" HasShadow="True" CornerRadius="10" Padding="10" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
                                <StackLayout>
                                    <Image Source="http://www.clker.com/cliparts/l/u/5/P/D/A/arrow-50x50-md.png"  WidthRequest="70" HeightRequest="70"  >
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer
                                                     Command="{Binding LoadElements}"

                                                       />
                                        </Image.GestureRecognizers>
                                    </Image>
                                    <Label Text="{Binding Title}" HeightRequest="50" WidthRequest="100" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                                </StackLayout>
                            </Frame>
                        </Frame>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

        </StackLayout>

xaml.cs中的代码:

[[XamlCompilation(XamlCompilationOptions.Compile)]公共局部类菜单:ContentPage{

    MenuViewModel viewModel = new MenuViewModel();
    public Menu()
    {
        InitializeComponent();
        BindingContext = viewModel;
    }



}

viewmodel.cs中的代码

 public class MenuViewModel :BaseViewModel, INotifyPropertyChanged
    {
        public Command LoadElements { get; set; }

        public ObservableCollection<Meni> MeniElementi { get; set; }
        public MenuViewModel()
        {

            LoadElements= new Command(execute: async () => await ExecuteElements());
            MeniElementi = new ObservableCollection<Meni>() {

            new Meni(){Title="Transatcions" ,Picture="xxx"},
            new Meni(){Title="Transatcions" ,Picture="xxx"},
       };
        }
        async Task ExecuteElements()
        {
            try
            {
                await Application.Current.MainPage.Navigation.PushAsync(new InfoPage());
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {     
            }
        }

LoadElements命令未触发。正在加载开始菜单,显示菜单元素只是命令不起作用,无法导航到另一个页面。使用Xamarin.Forms.Command。在其他页面上可以正常使用Command

xamarin xamarin.forms xamarin.android xamarin.ios
2个回答
0
投票

您需要在页面的ViewModel上调用此命令:-

<Image.GestureRecognizers>
       <TapGestureRecognizer Command="{Binding LoadElements, Source={x:Reference Name=ThisPage}}" />
</Image.GestureRecognizers>

然后将x:Name="ThisPage"放在页面顶部xaml的ContentPage标记中。

让我知道您是否遇到困难。


0
投票

您可以重置水龙头的装订路径。这样您就可以在同一命令中处理逻辑。

在ContentPage中(包含CollectionView)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Name="Page"   //set the name of page
             x:Class="xxx.xxxPage">
<TapGestureRecognizer Command="{Binding Source={x:Reference Page},Path=BindingContext.LoadElements}"
© www.soinside.com 2019 - 2024. All rights reserved.