首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xamarin自定义导航栏

xamarin自定义导航栏
EN

Stack Overflow用户
提问于 2018-03-11 11:47:27
回答 1查看 599关注 0票数 0

我试着在导航栏里有个搜索栏。我是用这个代码做的:

代码语言:javascript
复制
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继承的页面。

我也这样指出出口:

代码语言:javascript
复制
[assembly: ExportRenderer(typeof(NavigationSearchPage), typeof(NavigationSearchRenderer))]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-12 08:28:45

它可以工作,但现在所有导航栏都有搜索栏,我只想要从NavigationSearchPage继承的页面。

所有页面都有搜索栏,因为当您导航时,NavigationPage永远不会刷新。而且因为您的呈现程序只将SearchView添加到工具栏中,所以永远不会清除它。您添加的SearchView将始终存在。

解决方案:

  1. 修改此渲染器以呈现项目中的所有页面,您可以使用超类作为ExportRenderer的第一个参数。例如,如果您的所有页面都是ContentPage类型的,那么注册您的渲染器如下: 程序集:ExportRenderer(typeof(ContentPage),typeof(NavigationSearchRenderer))
  2. 修改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(子);}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49219641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档