【问题标题】:Search bar remove text is not working in xamarin ios搜索栏删除文本在 xamarin ios 中不起作用
【发布时间】:2018-03-26 06:26:08
【问题描述】:

我正在使用 xamarin 表单中的搜索栏。我无法通过单击十字按钮删除搜索栏中的搜索文本。我正在使用自定义渲染器来删除搜索栏中的取消文本。当我使用该渲染器时,我无法删除文本,如果我删除该渲染器,它就可以正常工作。我在渲染器文件中做错了什么。这是渲染器中隐藏搜索栏旁边的取消按钮的示例代码。

渲染器示例代码:

  protected override void OnElementPropertyChanged(object sender, PropertyChnagedEventArgs e)
  {

      Control.ShowCancelButton = false;
  }

当我尝试通过单击搜索栏中的交叉图像来删除搜索栏中的文本时,它在 xamarin ios 中不起作用,但在 ios 中运行良好。

示例代码:

 <StackLayout Grid.Column = "0" Orientation = "Horizontal">
 <Image Source = "backarrow.png" HorizontalOptions = "StartAndExpand" VerticalOptions = "CenterAndExpand" />                           
 <controls:CustomSearchbar x:Name = "CustomSearchbar" BackgroundColor ="Transparent" Text ="{Binding SearchTag}" SearchCommand ="{Binding RestaurantSearchCommand}" GHorizontalOptions = "StartAndExpand" VerticalOptions = "CenterAndExpand" >

这是我用于搜索栏的代码。这里的 'customsearchbar' 类继承自 searchrenderer。

【问题讨论】:

  • 您能否分享您用于调用搜索栏的 XAML 代码?
  • 嗨 cvanbeek,我正在添加我正在使用的示例代码。
  • 我无法重现您的问题。它适用于我的代码(有一些拼写错误)。我为你发布我的代码。希望对您有所帮助!

标签: xamarin xamarin.ios xamarin.forms


【解决方案1】:

对我来说很好用。

这是我的代码示例:

PCL 中的 MySearchBar.cs:

using Xamarin.Forms;

namespace Kevin_XF
{
    public class MySearchBar : SearchBar
    {

    }
}

iOS 平台中的 MySearchBarRenderer.cs:

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Kevin_XF.iOS;
using Kevin_XF;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(MySearchBar),typeof(MySearchBarRenderer))]
namespace Kevin_XF.iOS
{
    public class MySearchBarRenderer: SearchBarRenderer
    {

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control != null)
            {
                Control.ShowsCancelButton = false;
            }
        }
    }
}

MainPage.xaml 中的 Xaml 代码:

<StackLayout>
        <local:MySearchBar x:Name = "CustomSearchbar" BackgroundColor ="Transparent" HorizontalOptions = "StartAndExpand" VerticalOptions = "CenterAndExpand"  />
</StackLayout>

它的工作原理是这样的:

【讨论】:

  • @Deepak,我的回答对你有帮助吗?
猜你喜欢
  • 2020-03-02
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
相关资源
最近更新 更多