C# 向Xamarin.Forms Android和Iphone添加ViewCell公开指示器附件

C# 向Xamarin.Forms Android和Iphone添加ViewCell公开指示器附件,c#,xamarin,xamarin.forms,renderer,C#,Xamarin,Xamarin.forms,Renderer,我发现很多移动应用程序(Android和IOS)都有一个在最右边带有图标的列表视图,比如Exposition、checkmark等等,这就是call viewcell渲染器。 我知道如何在iphone上运行,但我不知道如何在android上运行? 感谢有人能与我分享如何将其应用到android。提前谢谢。 你可以看到我上传的图片如下: 我在iOS中添加了一个类文件,代码如下: using System; using System.Collections.Generic; using Syste

我发现很多移动应用程序(Android和IOS)都有一个在最右边带有图标的列表视图,比如Exposition、checkmark等等,这就是call viewcell渲染器。 我知道如何在iphone上运行,但我不知道如何在android上运行? 感谢有人能与我分享如何将其应用到android。提前谢谢。 你可以看到我上传的图片如下:

我在iOS中添加了一个类文件,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Foundation;
using UIKit;
using CustomRenderer.iOS;

[assembly: ExportRenderer(typeof(ViewCell), 
typeof(StandardViewCellRenderer))]
namespace CustomRenderer.iOS
{
class StandardViewCellRenderer : ViewCellRenderer
{
    public override UIKit.UITableViewCell GetCell(Cell item, 
UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        switch (item.StyleId)
        {
            case "none":
                cell.Accessory = UIKit.UITableViewCellAccessory.None;
                break;
            case "checkmark":
                cell.Accessory = 
UIKit.UITableViewCellAccessory.Checkmark;
                break;
            case "detail-button":
                cell.Accessory = 
UIKit.UITableViewCellAccessory.DetailButton;
                break;
            case "detail-disclosure-button":
                cell.Accessory = 
UIKit.UITableViewCellAccessory.DetailDisclosureButton;
                break;
            case "disclosure":
            default:
                cell.Accessory = 
UIKit.UITableViewCellAccessory.DisclosureIndicator;
                break;
        }
        return cell;
    }

 }
}

Then, in my main XAML, code as below:
<ListView x:Name="MyListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell StyleId="detail-disclosure-button">
                <StackLayout>
                    <Label Text="{Binding AnimalName}"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Xamarin.Forms.Platform.iOS;
使用Xamarin.Forms;
使用基础;
使用UIKit;
使用CustomRenderer.iOS;
[组件:导出渲染器(类型)(ViewCell),
类型(StandardViewCellRenderer))]
命名空间CustomRenderer.iOS
{
类StandardViewCellRenderer:ViewCellRenderer
{
公共覆盖UIKit.UITableViewCell GetCell(单元格项,
UIKit.UITableViewCell可重用单元,UIKit.UITableView电视)
{
var cell=base.GetCell(item、reusableCell、tv);
开关(item.StyleId)
{
案例“无”:
cell.accessority=UIKit.uitableviewCellAccessority.None;
打破
案例“复选标记”:
单元附件=
UIKit.UITableViewCellAccessory.Checkmark;
打破
案例“详情按钮”:
单元附件=
UIKit.UITableViewCellAccessory.DetailButton;
打破
案例“详细披露按钮”:
单元附件=
UIKit.UITableViewCellAccessory.DetailDisclosure按钮;
打破
“披露”一案:
违约:
单元附件=
UIKit.UITableViewCellAccessory.DisclosureIndicator;
打破
}
返回单元;
}
}
}
然后,在我的主要XAML中,代码如下:

对于Android,您需要下载一个图像(没有真正的原生方式,因为这是一个iOS设计功能)

首先下载相应的图标(例如从)

然后,使用自定义渲染器

使用Android.Graphics; 使用Android.Graphics.Drawables; 使用Android.Views; 使用Testapp.Droid; 使用Xamarin.Forms; 使用Xamarin.Forms.Platform.Android

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCellRenderer))]
namespace Testapp.Droid
{
    public class CustomViewCellRenderer : ViewCellRenderer
    {

        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
        {
            var cell = base.GetCellCore(item, convertView, parent, context);

            //switch (item.StyleId)
            //{
            //  case "disclosure":

            //      break;
            //}



            var bmp = BitmapFactory.DecodeResource(cell.Resources, Resource.Drawable.arrow);
            var bitmapDrawable = new BitmapDrawable(cell.Resources, Bitmap.CreateScaledBitmap(bmp, 50, 50, true));
            bitmapDrawable.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
            cell.SetBackground(bitmapDrawable);


            return cell;
        }
    }
}
然后,这个XAML:

<ListView>
                <ListView.ItemsSource>
                  <x:Array Type="{x:Type x:String}">
                    <x:String>mono</x:String>
                    <x:String>monodroid</x:String>
                    <x:String>monotouch</x:String>
                    <x:String>monorail</x:String>
                    <x:String>monodevelop</x:String>
                    <x:String>monotone</x:String>
                    <x:String>monopoly</x:String>
                    <x:String>monomodal</x:String>
                    <x:String>mononucleosis</x:String>
                  </x:Array>
                  </ListView.ItemsSource>
                  <ListView.ItemTemplate>
                     <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding .}" TextColor="#f35e20" />                             
                            </StackLayout>                           
                        </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

单声道
单机器人
单触式
单轨铁路
单发
单调的
垄断
单众数
单核细胞增多症
其结果是:


对于Android,您需要下载一个图像(没有真正的原生方式,因为这是一个iOS设计功能)

首先下载相应的图标(例如从)

然后,使用自定义渲染器

使用Android.Graphics; 使用Android.Graphics.Drawables; 使用Android.Views; 使用Testapp.Droid; 使用Xamarin.Forms; 使用Xamarin.Forms.Platform.Android

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCellRenderer))]
namespace Testapp.Droid
{
    public class CustomViewCellRenderer : ViewCellRenderer
    {

        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
        {
            var cell = base.GetCellCore(item, convertView, parent, context);

            //switch (item.StyleId)
            //{
            //  case "disclosure":

            //      break;
            //}



            var bmp = BitmapFactory.DecodeResource(cell.Resources, Resource.Drawable.arrow);
            var bitmapDrawable = new BitmapDrawable(cell.Resources, Bitmap.CreateScaledBitmap(bmp, 50, 50, true));
            bitmapDrawable.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
            cell.SetBackground(bitmapDrawable);


            return cell;
        }
    }
}
然后,这个XAML:

<ListView>
                <ListView.ItemsSource>
                  <x:Array Type="{x:Type x:String}">
                    <x:String>mono</x:String>
                    <x:String>monodroid</x:String>
                    <x:String>monotouch</x:String>
                    <x:String>monorail</x:String>
                    <x:String>monodevelop</x:String>
                    <x:String>monotone</x:String>
                    <x:String>monopoly</x:String>
                    <x:String>monomodal</x:String>
                    <x:String>mononucleosis</x:String>
                  </x:Array>
                  </ListView.ItemsSource>
                  <ListView.ItemTemplate>
                     <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding .}" TextColor="#f35e20" />                             
                            </StackLayout>                           
                        </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

单声道
单机器人
单触式
单轨铁路
单发
单调的
垄断
单众数
单核细胞增多症
其结果是:


如果您想在Android中实现这一点,我制作了一个示例,您可以从GitHub的ViewCell_Custom_Renderer文件夹下载以供参考

对于此示例,您需要首先创建一个自定义单元格

public class NativeCell : ViewCell
{
    public static readonly BindableProperty NameProperty =
        BindableProperty.Create("Name", typeof(string), typeof(NativeCell), "");

    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }

    public static readonly BindableProperty CategoryProperty =
        BindableProperty.Create("Category", typeof(string), typeof(NativeCell), "");

    public string Category
    {
        get { return (string)GetValue(CategoryProperty); }
        set { SetValue(CategoryProperty, value); }
    }

    public static readonly BindableProperty ImageFilenameProperty =
        BindableProperty.Create("ImageFilename", typeof(string), typeof(NativeCell), "");

    public string ImageFilename
    {
        get { return (string)GetValue(ImageFilenameProperty); }
        set { SetValue(ImageFilenameProperty, value); }
    }
}
然后在Android上创建自定义渲染器

   [assembly: ExportRenderer(typeof(NativeCell),       typeof(NativeAndroidCellRenderer))]
 namespace CustomRenderer.Droid
{
public class NativeAndroidCellRenderer : ViewCellRenderer
{
    NativeAndroidCell cell;

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
    {
        var nativeCell = (NativeCell)item;
        Console.WriteLine("\t\t" + nativeCell.Name);

        cell = convertView as NativeAndroidCell;
        if (cell == null)
        {
            cell = new NativeAndroidCell(context, nativeCell);
        }
        else
        {
            cell.NativeCell.PropertyChanged -= OnNativeCellPropertyChanged;
        }

        nativeCell.PropertyChanged += OnNativeCellPropertyChanged;

        cell.UpdateCell(nativeCell);
        return cell;
    }

    void OnNativeCellPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var nativeCell = (NativeCell)sender;
        if (e.PropertyName == NativeCell.NameProperty.PropertyName)
        {
            cell.HeadingTextView.Text = nativeCell.Name;
        }
        else if (e.PropertyName == NativeCell.CategoryProperty.PropertyName)
        {
            cell.SubheadingTextView.Text = nativeCell.Category;
        }
        else if (e.PropertyName == NativeCell.ImageFilenameProperty.PropertyName)
        {
            cell.SetImage(nativeCell.ImageFilename);
        }
    }
}
}

如果您想在Android中实现这一点,我制作了一个示例,您可以从GitHub的ViewCell_Custom_Renderer文件夹下载以供参考

对于此示例,您需要首先创建一个自定义单元格

public class NativeCell : ViewCell
{
    public static readonly BindableProperty NameProperty =
        BindableProperty.Create("Name", typeof(string), typeof(NativeCell), "");

    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }

    public static readonly BindableProperty CategoryProperty =
        BindableProperty.Create("Category", typeof(string), typeof(NativeCell), "");

    public string Category
    {
        get { return (string)GetValue(CategoryProperty); }
        set { SetValue(CategoryProperty, value); }
    }

    public static readonly BindableProperty ImageFilenameProperty =
        BindableProperty.Create("ImageFilename", typeof(string), typeof(NativeCell), "");

    public string ImageFilename
    {
        get { return (string)GetValue(ImageFilenameProperty); }
        set { SetValue(ImageFilenameProperty, value); }
    }
}
然后在Android上创建自定义渲染器

   [assembly: ExportRenderer(typeof(NativeCell),       typeof(NativeAndroidCellRenderer))]
 namespace CustomRenderer.Droid
{
public class NativeAndroidCellRenderer : ViewCellRenderer
{
    NativeAndroidCell cell;

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
    {
        var nativeCell = (NativeCell)item;
        Console.WriteLine("\t\t" + nativeCell.Name);

        cell = convertView as NativeAndroidCell;
        if (cell == null)
        {
            cell = new NativeAndroidCell(context, nativeCell);
        }
        else
        {
            cell.NativeCell.PropertyChanged -= OnNativeCellPropertyChanged;
        }

        nativeCell.PropertyChanged += OnNativeCellPropertyChanged;

        cell.UpdateCell(nativeCell);
        return cell;
    }

    void OnNativeCellPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var nativeCell = (NativeCell)sender;
        if (e.PropertyName == NativeCell.NameProperty.PropertyName)
        {
            cell.HeadingTextView.Text = nativeCell.Name;
        }
        else if (e.PropertyName == NativeCell.CategoryProperty.PropertyName)
        {
            cell.SubheadingTextView.Text = nativeCell.Category;
        }
        else if (e.PropertyName == NativeCell.ImageFilenameProperty.PropertyName)
        {
            cell.SetImage(nativeCell.ImageFilename);
        }
    }
}
}

我以前在stackoverlow中见过这种情况。所以,真的没有其他方法可以在android上获得这样的结果吗?要么这样,要么用XAML。没有其他方法。您好,先生,我不明白您的代码://switch(item.StyleId)//////////case“disclosure”://break;//}可以给我完整的代码吗?我以前在stackoverlow中见过这种方法。所以,真的没有其他方法可以在android上获得这样的结果吗?要么这样,要么用XAML。没有其他办法。您好,先生,我不明白您的代码://switch(item.StyleId)////////////case“disclosure”://break;//}可以给我完整的代码吗?