具有ViewCell项目选择的自定义渲染器颜色在iOS中工作,但在Xamarin Forms中不在Android中

时间:2017-06-26 10:45:38

标签: listview xamarin.ios xamarin.android xamarin.forms

我是Xamarin Forms的新手。我在PCL库中创建了一个自定义ViewCell,并为Droid和iOS提供了库参考。

PCL Library自定义类

using System;
using Xamarin.Forms;

namespace CustomLib
{
  public class ExtendedViewCell : ViewCell
   {
    public static readonly BindableProperty 
    SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor",
                                typeof(Color),
                                typeof(ExtendedViewCell),
                                Color.Default);

    public Color SelectedBackgroundColor
    {
        get { return (Color)GetValue(SelectedBackgroundColorProperty); }
        set { SetValue(SelectedBackgroundColorProperty, value); }
    }
  }
}

对于Android

using System;
using System.ComponentModel;
using Android.Content;
using Android.Graphics.Drawables;
using Android.Views;
using CustomLib;
using DiamondManagementApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace DiamondManagementApp.Droid
{
    public class ExtendedViewCellRenderer : ViewCellRenderer
    {

        private Android.Views.View _cellCore;
        private Drawable _unselectedBackground;
        private bool _selected;

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

            _selected = false;
            _unselectedBackground = _cellCore.Background;

            return _cellCore;
        }

        protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            base.OnCellPropertyChanged(sender, args);

            if (args.PropertyName == "IsSelected")
            {
                _selected = !_selected;

                if (_selected)
                {
                    var extendedViewCell = sender as ExtendedViewCell;
                    _cellCore.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
                }
                else
                {
                    _cellCore.SetBackground(_unselectedBackground);
                }
            }
        }
    }

}

适用于iOS

using System;
using CustomLib;
using DiamondManagementApp.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace DiamondManagementApp.iOS
{
public class ExtendedViewCellRenderer : ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);
        var view = item as ExtendedViewCell;



        cell.SelectedBackgroundView = new UIView
        {
            BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
        };

        return cell;
    }

}
}

并且在ListView的Binding中,下面的代码就在那里

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="DiamondManagementApp.MasterPage" 
    xmlns:control="clr-namespace:CustomLib;assembly=CustomLib"
    Title="Master Page">
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand"  Orientation="Vertical" Padding="0,20,0,0" BackgroundColor="#004F80">
            <ListView x:Name="listView" VerticalOptions="FillAndExpand" SeparatorVisibility="None" BackgroundColor="#0072BA">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <control:ExtendedViewCell SelectedBackgroundColor="Teal">
                        <ViewCell.View>
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding IconSource}" HeightRequest="24" WidthRequest="24" VerticalOptions="Center" Grid.Column="0" Margin="12,10,0,10" />
                                <Label Text="{Binding Title}" Style="{DynamicResource LabelStyle}" VerticalOptions="Center" HeightRequest="24" TextColor="White" Grid.Column="1" Margin="10,13,0,10" />
                            </StackLayout>
                        </ViewCell.View>
                            </control:ExtendedViewCell>

                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

此代码适用于iOS但在Android中它不会更改ListView项目选择的颜色。

任何帮助都要得到赞赏。

0 个答案:

没有答案
相关问题