如何在Xamarin Forms的NavigationBar中包含视图? [英] How to include view in NavigationBar of Xamarin Forms?

查看:435
本文介绍了如何在Xamarin Forms的NavigationBar中包含视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助!



我在跨平台的移动开发项目上工作。我使用Visual Studio与Xamarin Forms。我有一个SearchBar(Xamarin.Forms类),但是,我的问题是将它包含在NavigationBar中。



目前,我有一个MasterDetailPage并且我初始化了这个。详细信息使用新的NavigationPage(new customPage())。我也有一个SearchBar渲染器。
所以,我需要原始的NavigationBar(我想保留返回按钮),但我想在里面添加我的SearchBar。



如果我愿意,我将非常感激你知道一种方法,特别是对android。
提前谢谢!

解决方案

您需要创建一个自定义 NavigationRenderer 并将您自己的 UISearchBar 插入 UIToolbar SearchView 进入 ViewGroup



Xamarin表格:



注意:为了保持解耦,请使用 MessagingCenter 允许您在自定义渲染器中没有任何硬编码事件依赖项的情况下抽取消息。

  [assembly:ExportRenderer(typeof(NavigationPage),typeof(SeachBarNavigationRender))] 
namespace Forms.Life.Cycle.iOS
{
公共类SeachBarNavigationRender:NavigationRenderer
{
public SeachBarNavigationRender():base()
{
}

public override void PushViewController(UIViewController viewController) ,bool动画)
{
base.PushViewController(viewController,animated);
List< UIBarButtonItem> toolbarItem = new List< UIBarButtonItem> ();
foreach(TopViewController.NavigationItem.RightBarButtonItems中的UIBarButtonItem barButtonItem){
if(barButtonItem.Title.Contains(search)){
UISearchBar search = new UISearchBar(new CGRect(0,0) ,200,25));
search.BackgroundColor = UIColor.LightGray;
search.SearchBarStyle = UISearchBarStyle.Prominent;
search.TextChanged + =(object sender,UISearchBarTextChangedEventArgs e)=> {
D.WriteLine(e.SearchText);
MessagingCenter.Send< object,string> (这,SearchText,e.SearchText);
};
barButtonItem.CustomView = search;
}
toolbarItem.Add(barButtonItem);
}
TopViewController.NavigationItem.RightBarButtonItems = toolbarItem.ToArray();
}
}
}


I need your help !

I work on a mobile development project, cross-platform. I use Visual Studio with Xamarin Forms. I have a SearchBar (Xamarin.Forms class), but, my problem is to include this into the NavigationBar.

Currently, I have a MasterDetailPage and I init this.Detail with new NavigationPage(new customPage()). I have a SearchBar renderer, too. So, I need original NavigationBar (I want to keep the return button) but I would like to add my SearchBar inside.

I would be very grateful if you know of a way to do this especially to android. Thank you in advance !

解决方案

You need to create a custom NavigationRenderer and insert your own UISearchBar into the UIToolbar or SearchView into the ViewGroup:

Xamarin Forms : Renderer Base Classes and Native Controls

As an example of inserting a iOS UISearchBar into the nav bar via a custom NavigationRenderer:

Note: In order to keep things decoupled, using the MessagingCenter allows you to pump messages around without any hardcoding event dependancies within the custom renderer.

[assembly:ExportRenderer (typeof(NavigationPage), typeof(SeachBarNavigationRender))]
namespace Forms.Life.Cycle.iOS
{
    public class SeachBarNavigationRender : NavigationRenderer
    {
        public SeachBarNavigationRender () : base()
        {
        }

        public override void PushViewController (UIViewController viewController, bool animated)
        {
            base.PushViewController (viewController, animated);
            List<UIBarButtonItem> toolbarItem = new List<UIBarButtonItem> ();
            foreach (UIBarButtonItem barButtonItem in TopViewController.NavigationItem.RightBarButtonItems) {
                if ( barButtonItem.Title.Contains( "search")) {
                    UISearchBar search = new UISearchBar (new CGRect (0, 0, 200, 25));
                    search.BackgroundColor = UIColor.LightGray;
                    search.SearchBarStyle = UISearchBarStyle.Prominent;
                    search.TextChanged += (object sender, UISearchBarTextChangedEventArgs e) => {
                        D.WriteLine(e.SearchText);
                        MessagingCenter.Send<object, string> (this, "SearchText", e.SearchText);
                    };
                    barButtonItem.CustomView = search;
                }
                toolbarItem.Add (barButtonItem);
            }
            TopViewController.NavigationItem.RightBarButtonItems = toolbarItem.ToArray ();
        }
    }
}

这篇关于如何在Xamarin Forms的NavigationBar中包含视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆