Xamarin.Forms TapGestureRecognizer with Command navigate to page based on Image clicked

末鹿安然 提交于 2020-01-25 09:26:11

问题


Code in the xaml page:

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

Code in xaml.cs:

[XamlCompilation(XamlCompilationOptions.Compile)] public partial class Menu: ContentPage {

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



}

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

I have a menu made of Frames with Images and Texts. Also using Xamarin.Forms.Command. I need based on the image clicked to call a command and navigate to chosen Page


回答1:


You could use MessagingCenter to send message from ViewModel to ContentPage .

in command

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

in the ContentPage(which contains the CollectionView)

in the constructor

public xxxPage
{
  InitializeComponent();

  MessagingCenter.Subscribe<Object> (this, "openNewPage", async (sender) =>
  {
      await Navigation.PushAsync(new InfoPage());
  });
}



回答2:


I will suggest to you use this, In Xaml SelectionMode="Single" SelectedItems="{Binding SelectMenu}"

and use this in viewModel private Meni _selectMenu; public Meni SelectMenu { get { return _selectMenu; } set { _selectMenu = value; if(_selectMenu!=null) //navigate to other page OnPropertyChanged("SelectMenu"); } }



来源:https://stackoverflow.com/questions/59769664/xamarin-forms-tapgesturerecognizer-with-command-navigate-to-page-based-on-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!