Xamarin.IOS 在食谱中添加的 UIBarbuttonItem 不起作用。没有错误没有按钮

Xamarin.IOS UIBarbuttonItem added as in Recipe does not work. No error no button

使用从选项卡式应用程序模板派生的 Xamarin IOS 应用程序。定位 IOS 11+;在模拟器中测试 12.1.

我在其中一个 UIView 选项卡中添加了一个 UINavigationBar,并尝试在该栏中添加一个保存和取消按钮。

没有错误,没有喜悦,NavigationItem 不为空,并显示正确的导航项。这是大纲:

--UIViewController MyInfoController
----UIView InfoView
------UINavigationBar NavBar
--------UINavigationItem [no name]
------UIView ContentView
------A bunch of constraints
----UITabBar 

这里是代码的相关片段:

    public partial class MyInfoController : UIViewController
{
    protected MyInfoController(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        this.NavigationItem.SetRightBarButtonItem(
            new UIBarButtonItem(UIBarButtonSystemItem.Save, (sender, args) => 
            {
                // Handle Save Button Click here.
                //Create Alert
                var okAlertController = UIAlertController.Create("Click", "Right Button Clicked.", UIAlertControllerStyle.Alert);
                //Add Action
                okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                // Present Alert
                PresentViewController(okAlertController, true, null);
            }), true);


        // Try another way...

        var button = new UIBarButtonItem(UIBarButtonSystemItem.Cancel,  (sender, e) =>
        { 
            //Put Something here 
        });

        button.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = UIColor.White,
            TextShadowColor = UIColor.Clear
        }, UIControlState.Normal);

        this.NavigationItem.SetLeftBarButtonItem(button, true);
        // Create and add Views

没有错误消息问题。我没有看到任何错误,但我也没有看到栏按钮项。

我错过了什么? (:-S)

如果你使用ios设计器添加UINavigationBarUINavigationItem,你最好给它们设置名称,像这样

------UINavigationBar NavBar
--------UINavigationItem BarItem

所以,在控制器中,栏会正常显示。

 public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        this.BarItem.SetRightBarButtonItem(
        new UIBarButtonItem(UIBarButtonSystemItem.Save, (sender, args) =>
        {
            // Handle Save Button Click here.
            //Create Alert
            var okAlertController = UIAlertController.Create("Click", "Right Button Clicked.", UIAlertControllerStyle.Alert);
            //Add Action
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            // Present Alert
            PresentViewController(okAlertController, true, null);
        }), true);


        // Try another way...

        var button = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (sender, e) =>
        {
            //Put Something here 
        });

        button.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = UIColor.Black,
            TextShadowColor = UIColor.Clear
        }, UIControlState.Normal);

        this.BarItem.SetLeftBarButtonItem(button, true);
    }

更多信息:

如果你使用 this.NavigationItem ,根控制器中常用的是 UINavigationController .