带有命令的Xamarin.Forms TapGestureRecognizer导航到基于单击的图像的页面

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

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
            {     
            }
        }

我有一个由带有图像和文本的框架组成的菜单。还使用Xamarin.Forms.Command。我需要基于单击的图像来调用命令并导航到所选的页面

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

您可以使用MessagingCenter将消息从ViewModel发送到ContentPage。

在命令中

MessagingCenter.Send<Object>(this, "openNewPage");

在ContentPage(包含CollectionView)中

在构造函数中

public xxxPage
{
  InitializeComponent();

  MessagingCenter.Subscribe<Object> (this, "openNewPage", async (sender) =>
  {
      await Navigation.PushAsync(new InfoPage());
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.