Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms 如何更改xamarin表单中搜索栏搜索图标的颜色和取消按钮的颜色_Xamarin.forms_Searchbar - Fatal编程技术网

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

Xamarin.forms 如何更改xamarin表单中搜索栏搜索图标的颜色和取消按钮的颜色,xamarin.forms,searchbar,Xamarin.forms,Searchbar,如何从xaml更改搜索栏图标的颜色,我想在xamarin表单应用程序中更改搜索栏的取消和搜索图标的颜色。如何实现这一点。请提供此方面的帮助可以通过设置CancelButtonColor更改取消按钮的颜色: <SearchBar Placeholder="Search items..." CancelButtonColor="Orange" PlaceholderColor="Ora

如何从xaml更改搜索栏图标的颜色,我想在xamarin表单应用程序中更改搜索栏的取消和搜索图标的颜色。如何实现这一点。请提供此方面的帮助可以通过设置
CancelButtonColor
更改取消按钮的颜色:

        <SearchBar Placeholder="Search items..."
           CancelButtonColor="Orange"
           PlaceholderColor="Orange"
           TextColor="Orange"
           TextTransform="Lowercase"
           HorizontalTextAlignment="Center"
           FontSize="Medium"
           FontAttributes="Italic" />
编辑: 对于iOS,请尝试以下内容(我没有测试):

使用UIKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer(typeof(Xamarin.Forms.SearchBar)、typeof(MyApp.iOS.Renderers.iosearchBaricolorCustomRenderer))]
命名空间MyApp.iOS.Renderers
{
公共类iOSSearchBarIconColorCustomRenderer:SearchBallerDerrer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
var searchbar=(UISearchBar)控件;
searchbar.TintColor=UIColor.Orange;
}
}
}    
}

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

        <SearchBar Placeholder="Search items..."
           CancelButtonColor="Orange"
           PlaceholderColor="Orange"
           TextColor="Orange"
           TextTransform="Lowercase"
           HorizontalTextAlignment="Center"
           FontSize="Medium"
           FontAttributes="Italic" />
编辑: 对于iOS,请尝试以下内容(我没有测试):

使用UIKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer(typeof(Xamarin.Forms.SearchBar)、typeof(MyApp.iOS.Renderers.iosearchBaricolorCustomRenderer))]
命名空间MyApp.iOS.Renderers
{
公共类iOSSearchBarIconColorCustomRenderer:SearchBallerDerrer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
var searchbar=(UISearchBar)控件;
searchbar.TintColor=UIColor.Orange;
}
}
}    
}

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

[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);
            }
        }
    }
}
类似地,如果要在iOS中实现自定义搜索栏,还可以为其创建自定义渲染器

对于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);
            }
        }
    }
}
[程序集:导出呈现程序(typeof(MySearchBar)、typeof(mysearchbarnderer))]
名称空间searchbar.iOS
{
公共类mySearchBallenderer:SearchBallenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.BackgroundColor=UIColor.Yellow;
UISearchBar searchbar=控件作为UISearchBar;
searchbar.SearchTextField.LeftView.TintColor=UIColor.Orange;
//更新
var clearButton=searchbar.SearchTextField.ValueForKey((Foundation.NSString)“clearButton”)作为UIButton;
//clearButton.SetTitleColor(UIColor.Blue,UIControlState.Normal);
//clearButton.TintColor=UIColor.Orange;
clearButton.SetImage(UIImage.FromBundle(“CloseIcon.png”)、UIControlState.Normal);
}
}
}
}

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

[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);
            }
        }
    }
}
类似地,如果要在iOS中实现自定义搜索栏,还可以为其创建自定义渲染器

对于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);
            }
        }
    }
}
[程序集:导出呈现程序(typeof(MySearchBar)、typeof(mysearchbarnderer))]
名称空间searchbar.iOS
{
公共类mySearchBallenderer:SearchBallenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.BackgroundColor=UIColor.Yellow;
UISearchBar searchbar=控件作为UISearchBar;
searchbar.SearchTextField.LeftView.TintColor=UIColor.Orange;
//更新
var clearButton=searchbar.SearchTextField.ValueForKey((Foundation.NSString)“clearButton”)作为UIButton;
//clearButton.SetTitleColor(UIColor.Blue,UIControlState.Normal);
//clearButton.TintColor=UIColor.Orange;
clearButton.SetImage(UIImage.FromBundle(“CloseIcon.png”)、UIControlState.Normal);
}
}
}
}

如何在iosI添加的iOS渲染器中实现相同的功能。没有测试它,所以请尝试,如果它工作正常。我认为Kyle的iOS答案应该有效,我不知道你为什么会有问题。如何在iOS的iOS渲染器中实现同样的问题。没有测试它,所以请尝试,如果它工作正常。我认为Kyle的iOS答案应该有效,我不知道你为什么会有问题。但当我尝试更改取消图标颜色(X)时,就像这些搜索栏一样,SearchTextField.RightView.TintColor=UIColor.Orange,包含搜索栏的页面未加载。@Aswathy您可以通过“ValueForKey”获取“clearbutton”。在我的测试中,“色彩”不起作用。所以我改为设置按钮图像。非常好,2021年仍然有效,谢谢!但是,当我尝试更改取消图标的颜色(X)时,就像这些搜索栏一样,SearchTextField.RightView.TintColor=UIColor.Orange,包含搜索栏的页面未加载。@Aswathy您可以通过“ValueForKey”获取“clearbutton”。在我的测试中,“色彩”不起作用。所以我改为设置按钮图像。非常好,2021年仍然有效,谢谢!