【问题标题】:UITableViewRowAction not working on Xamarin formsUITableViewRowAction 不适用于 Xamarin 表单
【发布时间】:2020-03-14 12:53:30
【问题描述】:

当我试图通过向左滑动来删除表格视图中的一行时,滑动不起作用。有时在 10-12 次尝试后,表格视图行滑动,然后我可以看到删除按钮。下面是我正在使用的代码。请让我知道我哪里做错了?

注意:问题出现在 iPhone 上。在 iPad 上一切正常。

using System;
using System.Collections.Generic;
using System.Linq;
using Project.Controls;
using Project.Events;
using Project.Models;
using Foundation;
using Prism.Autofac;
using Prism.Events;
using Prism.Ioc;
using UIKit;

namespace Project.iOS.Renderers
{
    public class ExtendedListViewMPSource : UITableViewSource
    {
        IList<PlaylistModel> tableItems;
        ExtendedListViewMP listView;

        public IEnumerable<PlaylistModel> Items
        {
            //get{ }
            set => tableItems = value.ToList();
        }

        public ExtendedListViewMPSource(ExtendedListViewMP view)
        {
            tableItems = (IList<PlaylistModel>)view.Items.ToList();
            listView = view;
        }

        /// <summary>
        /// Called by the TableView to determine how many cells to create for that particular section.
        /// </summary>
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return tableItems.Count;
        }

        /// <summary>
        /// Called by the TableView to get the actual UITableViewCell to render for the particular section and row
        /// </summary>
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Request a recycled cell to save memory, if there are no cells to reuse, create a new one
            if (!(tableView.DequeueReusableCell(PlaylistViewCell.Key) is PlaylistViewCell cell))
            {
                cell = new PlaylistViewCell(PlaylistViewCell.Key);
                var views = NSBundle.MainBundle.LoadNib("PlaylistViewCell", cell, null);
                cell = ObjCRuntime.Runtime.GetNSObject(views.ValueAt(0)) as PlaylistViewCell;
            }

            var item = tableItems[indexPath.Row];
            cell.UpdateCellAsync(item);

            return cell;
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            var model = tableItems[indexPath.Row];

            if (listView.ListViewSelectionMode == ExtendedListViewSelectionMode.Multiple)
            {
                model.Selected = !model.Selected;
                NSIndexPath[] indexPaths = { indexPath };
                tableView.ReloadRows(indexPaths, UITableViewRowAnimation.None);
            }

            listView.NotifyItemSelected(model);
            tableView.DeselectRow(indexPath, true);
        }

        public override UITableViewRowAction[] EditActionsForRow(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewRowAction deleteButton = UITableViewRowAction.Create(
                UITableViewRowActionStyle.Destructive,
                "Delete",
                    DeleteRowActionHandler
                ); 

            return new UITableViewRowAction[] { deleteButton };
        }

        private void DeleteRowActionHandler(UITableViewRowAction rowAction, NSIndexPath indexPath)
        {
            if (Xamarin.Forms.Application.Current is PrismApplication prismApplication)
            {
                var eventAggregator = prismApplication.Container.Resolve<IEventAggregator>();
                if (eventAggregator != null)
                {
                    var playlistId = tableItems[indexPath.Row].Id;
                    eventAggregator.GetEvent<PlaylistDeleteEvent>().Publish(
                        new PlaylistDeleteEventParameters(playlistId, true, listView.ItemPlaylistType)
                    );
                }
            }
        }
    }
}

【问题讨论】:

  • 你的项目是 Xamarin.forms 项目还是 Xamarin.iOS 项目?
  • 你的代码看起来不错。您的 PlaylistViewCell 中是否有任何滑动手势?
  • @JackHua-MSFT :实际上 Master Detail Page 滑动手势已启用。禁用它有效。有什么方法可以仅在此页面上禁用 Master Detail Page Swipe 手势??
  • 在页面的OnAppearing方法中禁用它并在OnDisappearing方法中启用它。
  • 好的。谢谢@JackHua-MSFT

标签: c# xamarin xamarin.forms xamarin.ios prism


【解决方案1】:
 protected override void OnAppearing()
        {
            base.OnAppearing();

            if ((Application.Current.MainPage as MainMasterDetailPage) != null)
            {
                (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = false;
            }

        }

【讨论】:

    【解决方案2】:

    现在,如果用户没有从 Master Detail 页面导航怎么办。而如果 登录后直接加载页面

    OnAppearing 方法和OnDisappearing 将始终在页面可见或页面消失时触发。通过导航加载页面或在登录后直接加载都可以工作。

    您需要做的是在页面的 OnAppearing 方法中禁用它,并在评论中提到的 OnDisappearing 方法中启用它。

    protected override void OnAppearing()
    {
        base.OnAppearing();
        if ((Application.Current.MainPage as MainMasterDetailPage) != null)
        {
            (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = false;
        }
    }
    
    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        if ((Application.Current.MainPage as MainMasterDetailPage) != null)
        {
            (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 2017-06-05
      • 2017-10-01
      • 2023-01-24
      • 2021-01-15
      • 2019-12-06
      • 2019-04-29
      • 2018-10-30
      • 1970-01-01
      相关资源
      最近更新 更多