我试着在导航栏里有个搜索栏。我是用这个代码做的:
public class NavigationSearchRenderer : PageRenderer
{
private SearchView _searchView;
public NavigationSearchRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
var navPage = Element as NavigationSearchPage;
if (navPage == null)
return;
var activity = this.Context as FormsAppCompatActivity;
if (activity == null)
return;
var toolbar = activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
_searchView = new SearchView(Context);
toolbar.AddView(_searchView);
}
}它可以工作,但现在所有导航栏都有搜索栏,我只想要从NavigationSearchPage继承的页面。
我也这样指出出口:
[assembly: ExportRenderer(typeof(NavigationSearchPage), typeof(NavigationSearchRenderer))]发布于 2018-03-12 08:28:45
它可以工作,但现在所有导航栏都有搜索栏,我只想要从NavigationSearchPage继承的页面。
所有页面都有搜索栏,因为当您导航时,NavigationPage永远不会刷新。而且因为您的呈现程序只将SearchView添加到工具栏中,所以永远不会清除它。您添加的SearchView将始终存在。
解决方案:
ExportRenderer的第一个参数。例如,如果您的所有页面都是ContentPage类型的,那么注册您的渲染器如下:
程序集:ExportRenderer(typeof(ContentPage),typeof(NavigationSearchRenderer))OnElementChanged以在NavigationSearchPage时添加SearchView,在其他页面时清除SearchView:
保护覆盖OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e);var activity = this.Context as FormsAppCompatActivity;if (activity == null)返回;if (Element is NavigationSearchPage) { var工具栏=ElementChangedEventArgs _searchView =新的SearchView(上下文);toolbar.AddView(_searchView);{ var页=(元素为ContentPage);if (页== null) {返回;} var工具栏=== int count = toolbar.ChildCount;for (int i= 0;i< count;i++) { Android.Views.View子=toolbar.GetChildAt(i);如果(子为SearchView) {toolbar.RemoveView(子);}https://stackoverflow.com/questions/49219641
复制相似问题