【问题标题】:Xamarin.Forms TapGestureRecognizer with Command navigate to page based on Image clickedXamarin.Forms TapGestureRecognizer 使用命令导航到基于单击的图像的页面
【发布时间】:2020-01-16 12:24:14
【问题描述】:

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

    您可以使用 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());
      });
    }
    

    【讨论】:

      【解决方案2】:

      我会建议你使用这个, 在 Xaml 中

      SelectionMode="Single"
      SelectedItems="{Binding SelectMenu}"
      

      并在 viewModel

      中使用它
      private Meni _selectMenu;
      public Meni SelectMenu
          {
              get
              {
                  return _selectMenu;
              }
              set
              {
                  _selectMenu = value;
                  if(_selectMenu!=null)
                    //navigate to other page
                  OnPropertyChanged("SelectMenu");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        • 1970-01-01
        • 2022-01-04
        相关资源
        最近更新 更多