我们如何在Xamarin Forms上为IOS设置默认列表视图选择颜色?

时间:2018-04-18 03:42:56

标签: ios xamarin.forms

我在我的项目中尝试过这里所说的自定义渲染器 https://blog.falafel.com/adding-transparency-listview-ios-xamarin-forms-custom-renderer/

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/ios/theme 我将以下代码放在app delegate.cs文件的完成启动函数中   //开关

UISwitch.Appearance.OnTintColor = UIColor.FromRGB(0x91, 0xCA, 0x47); // green
UITableViewCell.Appearance.TintColor=UIColor.Yellow

但两者都无济于事。 UI上似乎没有任何变化,我不确定我是否错过了某些东西。有人能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我已经在网上尝试了一切可能的方式(我认为)。唯一有效的方法是制作一个带有触摸内部事件的BoxView,当用户触摸BoxView时会触发该事件。然后将BoxView添加到单元格的背景中(假设您正在为TableView或ListView使用自定义单元格)。之后,当用户触摸BoxView时,您必须更改BoxView的Color属性。

由于Xamarin表单除了点击之外不支持任何触摸手势。我们需要创建自己的。

  • 如何创建自定义BoxView

iOS渲染器(在Xamarin.iOS中):

using System;
using something;
using something.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MyBoxView), typeof(MyBoxViewRenderer))]
namespace something.iOS
{
    public class MyBoxViewRenderer : BoxRenderer
    {
        public MyBoxViewRenderer()
        {
        }

        public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, true);
        }

        public override void TouchesEnded(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, false);
        }

        public override void TouchesCancelled(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, false);
        }
    }
}

MyBoxView:

using System;
using Xamarin.Forms;

namespace something
{
    public class MyBoxView : BoxView
    {
        public event TouchChanged OnTouchChanged = delegate { };
        public delegate void TouchChanged(object sender, bool IsTouched);

        public void SendTouchEvent(object sender, bool IsTouched)
        {
            OnTouchChanged(sender, IsTouched);
        }

        public MyBoxView()
        {
        }
    }
}

希望有所帮助!

答案 1 :(得分:0)

您可以为ViewCell创建自定义渲染器,然后尝试更改所选颜色,如:

[assembly: ExportRenderer(typeof(ViewCell), typeof(MyViewCellRenderer))]
namespace ProjectName.iOS
{
    public class MyViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);

            cell.SelectedBackgroundView = new UIView(cell.Bounds);
            cell.SelectedBackgroundView.BackgroundColor = UIColor.Red;

            return cell;     
        }
    }
}