【问题标题】:Search Bar style changes in Xamarin FormsXamarin 表单中的搜索栏样式更改
【发布时间】:2018-04-04 07:12:30
【问题描述】:

我需要在我的 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);
            }
        }

Xamarin Android: 我使用框架控件在搜索栏中显示边框。我必须删除其中的搜索栏边框底线或边框颜色。

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

有人建议吗?

【问题讨论】:

  • 请发布您的代码。
  • @YorkShen-MSFT,在其中添加了更多详细信息。请就此提出建议。

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


【解决方案1】:

在 Android 中,您可以通过 id 找到 search_plate 并将其设置为 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);
} 

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

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;

    }
}

【讨论】:

  • 更改在 android 中运行良好。我已经在 Xamarin.iOS 的搜索栏渲染器的 OnElementChanged 事件中放入了提到的代码。但它不起作用。现在也取消在搜索栏中输入时显示的文本
  • 改用OnElementPropertyChanged。我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 2018-10-14
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多