如何在xamarin跨平台中实现Listview组件,而不是左右滑动的上下文操作?

如何在xamarin跨平台中实现Listview组件,而不是左右滑动的上下文操作?,listview,xamarin.forms,cross-platform,menuitem,Listview,Xamarin.forms,Cross Platform,Menuitem,SWTableViewCell可用于Xamarin iOS平台。但我希望Android和iOS Tableview都能使用Xamarin表单定制左右滑动操作 如果我需要为这两种平台创建自定义渲染器,如何实现这一点?您必须创建自定义单元视图渲染器,我这样做了,但有局限性,我使用破坏性背景和正常背景,我正在尝试获得与名称或其他字段相关的内容,你需要的是破坏性的背景,以下是我所做的: 在IOS项目中,添加一个文件CustomCellViewRenderer.cs,其代码如下: public class

SWTableViewCell可用于Xamarin iOS平台。但我希望Android和iOS Tableview都能使用Xamarin表单定制左右滑动操作


如果我需要为这两种平台创建自定义渲染器,如何实现这一点?

您必须创建自定义单元视图渲染器,我这样做了,但有局限性,我使用破坏性背景和正常背景,我正在尝试获得与名称或其他字段相关的内容,你需要的是破坏性的背景,以下是我所做的: 在IOS项目中,添加一个文件CustomCellViewRenderer.cs,其代码如下:

public class CustomCellViewRenderer : ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        try
        {
            var cell = base.GetCell(item, reusableCell, tv);
            CGRect rect = new CGRect(0, 0, 1, 1);
            CGSize size = rect.Size;
            UIGraphics.BeginImageContext(size);
            CGContext currentContext = UIGraphics.GetCurrentContext();
            currentContext.SetFillColor(Color.Green.ToCGColor());
            currentContext.FillRect(rect);
            var backgroundImage = UIGraphics.GetImageFromCurrentImageContext();
            currentContext.Dispose();
            CGContext currentNormalContext = UIGraphics.GetCurrentContext();
            currentNormalContext.SetFillColor(Color.FromRgb(220, 220, 0).ToCGColor());
            currentNormalContext.FillRect(rect);
            var normalBackgroundImage = UIGraphics.GetImageFromCurrentImageContext();
            currentNormalContext.Dispose();

            var t = Type.GetType("Xamarin.Forms.Platform.iOS.ContextActionsCell, Xamarin.Forms.Platform.iOS, Version=1.3.1.0, Culture=neutral, PublicKeyToken=null");

            var destructive = t.GetField("DestructiveBackground", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            var normal = t.GetField("NormalBackground", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            if (destructive != null)
                destructive.SetValue(null, backgroundImage);
            if (normal != null)
                normal.SetValue(null, normalBackgroundImage);

            return cell;
        }
        catch(Exception ex)
        {
            var ret = new UITableViewCell();
            return ret;
        }
    }
}
我在共享项目中有一个构造函数CustomCellView.cs:

public class CustomCellView : ViewCell
{
    public CustomCellView()
    {

    }
}
以及以下观点:

<MenuItem Command="{Binding Path=BindingContext.OnDone, Source={x:Reference Name=Steps}}" 
                                  CommandParameter="{Binding .}"
                                  Text="Complete" 
                                  IsDestructive="True"
                                  x:Name="Complete" />
                        <MenuItem Command="{Binding Path=BindingContext.OnStart, Source={x:Reference Name=Steps}}" 
                                  CommandParameter="{Binding .}"
                                  Text="Start" 
                                  IsDestructive="False"
                                  x:Name="Start"/>

我想说的是,我在Xamarin论坛上找到了所有这些代码,并从多个来源中获取了一些信息,它可以工作,但只允许2种颜色

我希望这能有所帮助。

可能的副本