简体   繁体   English

如何处理Xamarin.Forms中的iOS上的附件按钮点击?

[英]How to handle accessory button tap on iOS in Xamarin.Forms?

There's a AccessoryButtonTapped method to override in table view delegate, but it's not clear how to perform that in a ListViewRenderer subclass? 有一个AccessoryButtonTapped方法可在表视图委托中重写,但是尚不清楚如何在ListViewRenderer子类中执行该方法?

So I can display a disclosure indicator, but can't handle tap on it. 因此,我可以显示一个披露指示器,但不能处理它的轻击。

public class ContactCellRenderer : ImageCellRenderer
{
    public override UITableViewCell GetCell (
        Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell (item, reusableCell, tv);
        cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
        return cell;
    }
}

I think, you have just to implement the method AccessoryButtonTapped in your renderer. 我认为,您只需在渲染器中实现AccessoryButtonTapped方法即可。

public class ContactListViewRenderer : ListViewRenderer, IUITableViewDelegate
{
    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            Control.WeakDelegate = this; // or. Control.Delegate
        }
    }

    public virtual void AccessoryButtonTapped(UITableView tableView, NSIndexPath indexPath)
    {
        // accessory tapped
    }
}

In addition to Sven-Michael, you can enrich his code by creating a inheritance of your ListView (if you do not already have one) and add a Delegate to it like this: 除了Sven-Michael,您还可以通过创建ListView的继承关系(如果您还没有继承)来丰富他的代码,并向其添加Delegate ,如下所示:

public class AccessoryListView : ListView
{
   public delegate void OnAccessoryTappedDelegate();

   public OnAccessoryTappedDelegate OnAccessoryTapped { get; set; }
}

Now from your custom renderer - don't forget to set it to your new inherited ListView - call the delegate 现在从您的自定义渲染器开始-不要忘记将其设置为新的继承的ListView调用委托

public class ContactListViewRenderer : ListViewRenderer, IUITableViewDelegate
{
    private AccessoryListView _formsControl;

    protected override void OnElementChanged(ElementChangedEventArgs<AccessoryListView> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            Control.WeakDelegate = this; // or. Control.Delegate
        }

        if (e.NewElement != null)
           _formsControl = e.NewElement;
    }

    public virtual void AccessoryButtonTapped(UITableView tableView, NSIndexPath indexPath)
    {
        // accessory tapped
        if (_formsControl.OnAccessoryTapped != null)
           _formsControl.OnAccessoryTapped();
    }
}

You can of course add some parameters in there to supply your shared code with more data. 您当然可以在其中添加一些参数,以为共享代码提供更多数据。 With this you do have some platform specific code, but you get back to your shared code 'as soon as possible' making your code more reusable. 有了这个,您确实有一些特定于平台的代码,但是您可以“尽快”回到共享代码,从而使代码更具可重用性。

Another sample of this with a Map control can be found here . 另一个带有Map控件的示例可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM