如何在 xamarin 表单中更改搜索栏搜索图标的颜色和取消按钮颜色

How to change the color of searchbar search icon and cancel button color in xamarin forms

如何从 xaml 更改搜索栏图标的颜色,我想更改 xamarin 表单中搜索栏的取消和搜索图标颜色 application.How 以实现此目的。请对此提供帮助

取消按钮的颜色可以通过设置CancelButtonColor:

来改变
        <SearchBar Placeholder="Search items..."
           CancelButtonColor="Orange"
           PlaceholderColor="Orange"
           TextColor="Orange"
           TextTransform="Lowercase"
           HorizontalTextAlignment="Center"
           FontSize="Medium"
           FontAttributes="Italic" />

对于图标颜色,您需要使用自定义渲染器。 例如,在 Android 上,您可以创建一个新文件(类似于 SearchBar.Droid.cs)并在其中添加:

using Android.Content;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Xamarin.Forms.SearchBar), typeof(MyApp.Renderers.SearchBarIconColorCustomRenderer))]
namespace MyApp.Renderers
{
    public class SearchBarIconColorCustomRenderer : SearchBarRenderer
    {
        public SearchBarIconColorCustomRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            var icon = Control?.FindViewById(Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null));
            (icon as ImageView)?.SetColorFilter(Color.Orange.ToAndroid());
        }
    }
}

编辑: 对于 iOS,尝试这样的操作(我没有测试):

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

[assembly: ExportRenderer(typeof(Xamarin.Forms.SearchBar), typeof(MyApp.iOS.Renderers.iOSSearchBarIconColorCustomRenderer))]
namespace MyApp.iOS.Renderers
{
        public class iOSSearchBarIconColorCustomRenderer : SearchBarRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);
                
                if (Control != null)
                {
                    var searchbar = (UISearchBar)Control;
                    searchbar.TintColor = UIColor.Orange;
                }
            }
        }    
}

正如adamm所说,您可以通过CancelButtonColor修改“取消按钮颜色”。

同样,如果你想在iOS中实现一个自定义的SearchBar,你也可以为它创建一个自定义的渲染器。

对于UISearchBar,可以通过SearchTextField.LeftView.TintColor修改图标的颜色。

[assembly: ExportRenderer(typeof(MySearchBar), typeof(MySearchBarRenderer))]
namespace searchbar.iOS
{
    public class MySearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.BackgroundColor = UIColor.Yellow;
                UISearchBar searchbar = Control as UISearchBar;
                searchbar.SearchTextField.LeftView.TintColor = UIColor.Orange;
                // UPDATE
                var clearButton = searchbar.SearchTextField.ValueForKey((Foundation.NSString)"clearButton") as UIButton;
                //clearButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
                //clearButton.TintColor = UIColor.Orange;
                clearButton.SetImage(UIImage.FromBundle("CloseIcon.png"), UIControlState.Normal);
            }
        }
    }
}