我想使用基于SearchBarRenderer属性的自定义IsFocused来隐藏IOS中的搜索栏图标。我怎样才能做到这一点?
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null || Control == null)
return;
var element = Element as CustomSearchBar;
if (element.IsFocused)
{
}
else
{
}
}
发布于 2019-10-16 19:00:23
如果您正在使用自定义渲染器来实现它,请编写如下代码。
[assembly: ExportRenderer(typeof(CustomSearchBar), typeof(CustomSearchBarRenderer))]
namespace App15.iOS
{
public class CustomSearchBarRenderer : SearchBarRenderer
{
UIImage searchImg;
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
searchImg = Control.GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal);
if (e.OldElement != null)
{
Control.OnEditingStarted -= Control_OnEditingStarted;
Control.OnEditingStopped -= Control_OnEditingStopped;
}
if (e.NewElement != null)
{
if (null != Control)
{
Control.OnEditingStarted += Control_OnEditingStarted;
Control.OnEditingStopped += Control_OnEditingStopped;
}
}
}
private void Control_OnEditingStopped(object sender, EventArgs e)
{
var searchBar = sender as UISearchBar;
searchBar.SetImageforSearchBarIcon(searchImg, UISearchBarIcon.Search, UIControlState.Normal);
}
private void Control_OnEditingStarted(object sender, EventArgs e)
{
var searchBar = sender as UISearchBar;
searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
}
}
}
注意: App15是本地项目中的命名空间。
正如document 效果导论 of Effect所说的那样,您也可以参考这个文档产生效果来实现它,因为它不需要使用自定义渲染器来实现它。
效果样本如下:
创建SearchBarEffects:
public class SearchBarEffects : RoutingEffect
{
public SearchBarEffects() : base($"MyCompany.{nameof(SearchBarEffects)}")
{
}
}
在Xaml中使用:
// here is custom renderer
<local:CustomSearchBar Placeholder="renderer input"/>
// here is custom effect
<SearchBar Placeholder="effect input">
<SearchBar.Effects>
<local:SearchBarEffects />
</SearchBar.Effects>
</SearchBar>
在SearchBarEffect解决方案中创建iOS:
[assembly: ResolutionGroupName("MyCompany")]
[assembly: ExportEffect(typeof(App15.iOS.SearchBarEffect), "SearchBarEffects")]
namespace App15.iOS
{
public class SearchBarEffect : PlatformEffect
{
UIImage searchImg;
protected override void OnAttached()
{
searchImg = ((UISearchBar)Control).GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal);
((UISearchBar)Control).OnEditingStarted += SearchBar_OnEditingStarted;
((UISearchBar)Control).OnEditingStopped += SearchBar_OnEditingStopped;
}
private void SearchBar_OnEditingStopped(object sender, EventArgs e)
{
var searchBar = sender as UISearchBar;
searchBar.SetImageforSearchBarIcon(searchImg, UISearchBarIcon.Search, UIControlState.Normal);
}
private void SearchBar_OnEditingStarted(object sender, EventArgs e)
{
var searchBar = sender as UISearchBar;
searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
}
protected override void OnDetached()
{
((UISearchBar)Control).OnEditingStarted -= SearchBar_OnEditingStarted;
((UISearchBar)Control).OnEditingStopped -= SearchBar_OnEditingStopped;
}
}
}
最后,以两种方式展示效果。上是效果的,下是伦德尔的。
发布于 2019-10-16 03:59:44
个人注意-没有必要使用自定义渲染器,一个效果是远远超过-我测试的效果,而不是自定义渲染器。关于如何创建效果,请参见这里
如果只将此效果添加到<SearchBar>
元素.中,则this.Control
将成为UISearchBar
。
因此您可以这样做(基于iOS和Xamarin.iOS文档):
效果的iOS实现:
public class NoSearchIconEffect : PlatformEffect
{
UIImage defaultIcon = null;
protected override void OnAttached()
{
var searchBar = this.Control as UISearchBar;
defaultIcon = defaultIcon ?? searchBar.GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal); //This will save the default icon
searchBar.OnEditingStarted += delegate
{
searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
};
searchBar.OnEditingStopped += delegate
{
searchBar.SetImageforSearchBarIcon(defaultIcon, UISearchBarIcon.Search, UIControlState.Normal);
};
}
protected override void OnDetached()
{
}
}
https://stackoverflow.com/questions/58412605
复制相似问题