gpt4 book ai didi

c# - Xamarin IOS 基于自定义渲染器中的 IsFocused 属性隐藏搜索栏图标

转载 作者:行者123 更新时间:2023-11-30 22:52:12 25 4
gpt4 key购买 nike

我想使用基于 IsFocused 属性的自定义 SearchBarRenderer 在 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
{

}
}

最佳答案

如果你使用Custom Renderer来实现它,代码如下。

[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 是我本地项目中的命名空间。

作为文档 Introduction to Effects 的效果说,你也可以引用这个文档Creating an Effect使用 Effect 来实现它,因为它不需要使用 Custom Renderer 来实现它。

效果示例如下:

创建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>

在 iOS 解决方案中创建 SearchBarEffect:

[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;
}
}
}

最后,通过两种方式展示效果。向上是 Effect 的,向下是 Renderer 的。

enter image description here

关于c# - Xamarin IOS 基于自定义渲染器中的 IsFocused 属性隐藏搜索栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com