将UIBarButtonItem添加到Xamarin IOS中的导航栏中

本文关键字:导航 IOS Xamarin UIBarButtonItem 添加 | 更新日期: 2023-09-27 18:20:24

如何将自定义UIBarButtonItem添加到导航栏右侧。

public UIBarButtonItem btn_Cart {get; set;}
public DetailedController (){
string buttonTitle = "One";
btn_Cart = new UIBarButtonItem (buttonTitle, UIBarButtonItemStyle.Bordered, null);
btn_Cart.Clicked += (s, e) => { new UIAlertView ("click!", "btnOne clicked", null, "OK", null).Show (); 
};
}
public override void ViewDidLoad (){
base.ViewDidLoad ();
// Added btn_Cart to RightSide Of Naviation Bar.
this.NavigationItem.SetRightBarButtonItem (btn_Cart, true);
}

它未添加到导航项中。

如果我将代码更改为

public override void ViewDidLoad (){
this.NavigationItem.SetRightBarButtonItem(
                new UIBarButtonItem(UIImage.FromBundle ("Image.png")
                    , UIBarButtonItemStyle.Plain
                    , (sender,args) => {
new UIAlertView ("click!", "btnOne clicked", null, "OK", null).Show ();
                    })
                , true);
}

//它添加到右侧的NavigationItem中。

public class DetailedTabBarController : UITabBarController {
        public override void ViewDidLoad (){
            base.ViewDidLoad ();
            this.NavigationItem.SetRightBarButtonItem(
            new UIBarButtonItem(UIImage.FromBundle ("/Images/Image.png"),
                    UIBarButtonItemStyle.Plain, 
                    (sender,args) => {
                Console.WriteLine("Do Some Action!");
                }), true);
            }
}

我不想这样补充。

有人能建议我把UIBarButtonItem添加到右侧的NavigationItem中吗。@全部感谢提前

将UIBarButtonItem添加到Xamarin IOS中的导航栏中

首先创建一个UIBarButtonItem。然后将其添加到NavigationItem中。这是的样本代码

UIBarButtonItem btn = new UIBarButtonItem ();
btn.Image = UIImage.FromFile("Image.gif");
btn.Clicked += (sender , e)=>{System.Diagnostics.Debug.WriteLine("Button tap");};
NavigationItem.RightBarButtonItem = btn;