我可以在 Xamarin.iOS 中制作 UIMenu 吗?

问题描述

我在 Xamarin.iOS 中开发。我想创建一个下拉菜单,我在 Apple Human Interface Guidelines 中看到了它。 但我看不到它official Microsoft documentation

是否在 Xamarin.iOS 中实现?如果是,我该如何使用它?

解决方法

您可以检查以下代码

        UIButton button = new UIButton()
        {
            Frame = new CoreGraphics.CGRect(30,100,200,80),Menu = CreateMenu(),};

        button.SetTitle("Click Test",UIControlState.Normal);
        button.SetTitleColor(UIColor.SystemRedColor,UIControlState.Normal);
        button.ShowsMenuAsPrimaryAction = true;
        View.AddSubview(button);
       UIMenu CreateMenu()
        {
            
            //you can set the icon as the second parameter
            var Action1 = UIAction.Create("Action1",null,(arg)=> { 
            
                // do something when you click this item

            });

            var Action2 = UIAction.Create("Action2",(arg) => {

                // do something when you click this item

            });

            var Action3 = UIAction.Create("Action3",(arg) => {

                // do something when you click this item

            });

            var Menus = UIMenu.Create(new UIMenuElement[] {Action1,Action2,Action3 });
            return Menus;
        }

enter image description here