Xamarin.ios Xamarin表单中搜索栏样式的更改

Xamarin.ios Xamarin表单中搜索栏样式的更改,xamarin.ios,xamarin.forms,xamarin.android,Xamarin.ios,Xamarin.forms,Xamarin.android,我需要在Xamarin Android和Xamarin iOS的应用程序中使用搜索栏 我必须在我的应用程序中实现以下搜索栏 请查找xaml中使用的代码 <Frame Padding="0" OutlineColor="DarkGray" HasShadow="True" HorizontalOptions="FillAndExpand" VerticalOptions="Center"> <SearchBar x:Name="sea

我需要在Xamarin Android和Xamarin iOS的应用程序中使用搜索栏

我必须在我的应用程序中实现以下搜索栏

请查找xaml中使用的代码

<Frame Padding="0" OutlineColor="DarkGray" HasShadow="True" HorizontalOptions="FillAndExpand"  VerticalOptions="Center">
                    <SearchBar x:Name="searchBar" Placeholder="Search" PlaceholderColor="LightGray" TextColor="#000000" HorizontalOptions="FillAndExpand" VerticalOptions="Center" TextChanged="SearchBar_TextChanged"/>
                </Frame>

我的搜索栏如下所示,需要删除xamarin android中突出显示的行。 还可以查找搜索条形码

protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            var color = global::Xamarin.Forms.Color.LightGray;
            var searchView = (Control as SearchView);
            var searchIconId = searchView.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
            if (searchIconId > 0)
            {
                var searchPlateIcon = searchView.FindViewById(searchIconId);
                (searchPlateIcon as ImageView).SetColorFilter(color.ToAndroid(), PorterDuff.Mode.SrcIn);
            }
            var symbolView = (Control as SearchView);
            var symbolIconId = symbolView.Resources.GetIdentifier("android:id/search_close_btn", null, null);
            if(symbolIconId>0)
            {
                var symbolPlateIcon = symbolView.FindViewById(symbolIconId);
                (symbolPlateIcon as ImageView).SetColorFilter(color.ToAndroid(), PorterDuff.Mode.SrcIn);
            }
        }
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var color=global::Xamarin.Forms.color.LightGray;
var searchView=(控件为searchView);
var searchIconId=searchView.Resources.GetIdentifier(“android:id/search\u mag\u icon”,null,null);
如果(searchIconId>0)
{
var searchplatecon=searchView.findviewbyd(searchIconId);
(searchPlateIcon作为ImageView.SetColorFilter(color.ToAndroid(),PorterDuff.Mode.SrcIn);
}
var symbolView=(控件为SearchView);
var symbolicond=symbolView.Resources.GetIdentifier(“android:id/search\u close\u btn”,null,null);
如果(符号ID>0)
{
var SYMBOLPLATECON=symbolView.FindViewById(SYMBOLCONID);
(symbolPlateIcon作为ImageView.SetColorFilter(color.ToAndroid(),PorterDuff.Mode.SrcIn);
}
}
Xamarin Android: 我使用了框架控件在搜索栏中显示边框。我必须删除它的搜索栏边框底线或边框颜色

Xamarin iOS: 我必须实现搜索栏控制,如图所示。在搜索时,我必须删除搜索栏中显示的“取消”字。还需要删除它周围的半径


有人对此提出建议吗?

在安卓系统中,您可以通过
id
找到搜索板,并将其设置为
Transparent
,如下所示:

if (Control != null)
{
        var color = global::Xamarin.Forms.Color.LightGray;
        var searchView = Control as SearchView;

        int searchPlateId = searchView.Context.Resources.GetIdentifier("android:id/search_plate", null, null);
        Android.Views.View searchPlateView = searchView.FindViewById(searchPlateId);
        searchPlateView.SetBackgroundColor(Android.Graphics.Color.Transparent);
} 
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (Control != null)
    {
        Control.ShowsCancelButton = false;

        UITextField txSearchField = (UITextField)Control.ValueForKey(new Foundation.NSString("searchField"));
        txSearchField.BackgroundColor = UIColor.White;
        txSearchField.BorderStyle = UITextBorderStyle.None;
        txSearchField.Layer.BorderWidth = 1.0f;
        txSearchField.Layer.CornerRadius = 2.0f;
        txSearchField.Layer.BorderColor = UIColor.LightGray.CGColor;

    }
}
在iOS中,您可以找到UISearchBar的文本字段,然后自定义其边框样式。并通过将
ShowsCancelButton
设置为false来移除“取消”按钮。例如,像这样:

if (Control != null)
{
        var color = global::Xamarin.Forms.Color.LightGray;
        var searchView = Control as SearchView;

        int searchPlateId = searchView.Context.Resources.GetIdentifier("android:id/search_plate", null, null);
        Android.Views.View searchPlateView = searchView.FindViewById(searchPlateId);
        searchPlateView.SetBackgroundColor(Android.Graphics.Color.Transparent);
} 
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (Control != null)
    {
        Control.ShowsCancelButton = false;

        UITextField txSearchField = (UITextField)Control.ValueForKey(new Foundation.NSString("searchField"));
        txSearchField.BackgroundColor = UIColor.White;
        txSearchField.BorderStyle = UITextBorderStyle.None;
        txSearchField.Layer.BorderWidth = 1.0f;
        txSearchField.Layer.CornerRadius = 2.0f;
        txSearchField.Layer.BorderColor = UIColor.LightGray.CGColor;

    }
}

请发布您的代码。@YorkShen MSFT,在其中添加了更多详细信息。请对此提出建议。在安卓系统中,这些更改运行良好。我已经将上述代码放在Xamarin.iOS搜索栏渲染器的OnElementChanged事件中。但它不起作用。现在也可以取消在搜索栏中输入时显示的文本,改为使用OnElementPropertyChanged。我已经更新了我的答案。