【问题标题】:Xamarin.Forms GestureRecognizers not working on CommandXamarin.Forms GestureRecognizers 不适用于命令
【发布时间】:2020-01-16 11:10:34
【问题描述】:

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。在使用命令正常工作的其他页面上

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    您可以重置 tap 的绑定路径。这样您就可以在同一命令中处理逻辑。

    在 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}"
    

    【讨论】:

    • 我在两个地方添加了随机名称 ex.Refresh,现在正在工作
    • 我还有一个问题。因为这是菜单,如何在 ViewModel 中使用 async Task ExecuteElements() 中的此命令根据单击的菜单项(图像)导航到新页面?
    • 您可以在命令中使用 MessagingCenter 将消息从 ViewModel 发送到 ContentPage。检查docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
    • 我有点迷茫。在哪里订阅,在哪里发送以及什么?发送标题并根据标题移动到所选页面或?
    • 我应该打开新问题吗?
    【解决方案2】:

    您需要在页面的 ViewModel 上调用命令来执行此操作:-

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

    并将x:Name="ThisPage"放在页面顶部xaml的ContentPage标签中。

    如果您遇到困难,请告诉我。

    【讨论】:

    • 我不明白要添加哪个页面?
    • 您需要编辑您提供的代码。并且 x:Name="ThisPage" 需要添加到同一个 ContentPage
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2011-03-28
    • 1970-01-01
    • 2016-07-17
    • 2021-07-01
    • 2020-09-29
    • 2013-07-09
    相关资源
    最近更新 更多