【问题标题】:Replicating the drop-down ToolbarItem "More" in Xamarin.Forms在 Xamarin.Forms 中复制下拉 ToolbarItem“更多”
【发布时间】:2018-01-06 15:15:44
【问题描述】:

ToolbarItem 的订单设置为 IOS 的 Secondary 时,我正在努力如何从 Xamarin.Forms 复制下拉 ToolbarItem,以使其看起来像在 Android 上一样。


这里有一些图片可以更好地解释我在寻找什么:

在 Android 上的工作原理:

  1. 代码:
ToolbarItem toolbarItem = new ToolbarItem()
{
  Text = "ToolbarItem",
  Order = ToolbarItemOrder.Secondary
};
  1. 在 Android 上的外观图片:

显示“更多”图标的图片

显示“更多”图标的图像已展开以显示更多工具栏项

在 iOS 中将Order 设置为Secondary 时,工具栏上没有默认的“更多”图标。相反,会在导航栏下方创建一个栏,其中包括所有工具栏项——我不希望我的应用程序拥有这些项。


这是以前在 IOS 上如何实现的示例:

我从我的一个应用中截取的屏幕截图,它实现了这一点 效果

【问题讨论】:

  • 如您所见,很多人都有这个问题:forums.xamarin.com/discussion/comment/314600#Comment_314600
  • 您可以使用自定义渲染器来实现这一点。
  • @TreviAwater 请解释一下如何?
  • 可能有点晚了,但这可能会有所帮助。 github.com/SKLn-Rad/Xam.Plugin.PopupMenu
  • @PaulaKristin 谢谢!

标签: android ios xamarin xamarin.forms toolbaritems


【解决方案1】:

在原生iOS中,你可以使用UIPopoverController来实现你的效果。但请注意,此控件只能在 iPad 上使用。

由于您使用的是 Xamarin.Forms,我们可以在 iOS 平台中创建自定义渲染器来获取此内容。

首先,创建一个页面渲染器来显示UIPopoverController。我们可以根据您的要求从 UIBarButtonItem 或 UIView 显示它。在这里,我使用 UIBarButtonItem 之类的:

//I defined the navigateItem in the method ViewWillAppear
public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    rightItem = new UIBarButtonItem("More", UIBarButtonItemStyle.Plain, (sender, args) =>
    {
        UIPopoverController popView = new UIPopoverController(new ContentViewController());
        popView.PopoverContentSize = new CGSize(200, 300);
        popView.PresentFromBarButtonItem(rightItem, UIPopoverArrowDirection.Any, true);
    });

    NavigationController.TopViewController.NavigationItem.SetRightBarButtonItem(leftItem, true);
}

其次,在UIPopoverController中构造内容ViewController(就像android中的二级列表一样):

public class ContentViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        UITableView tableView = new UITableView(new CGRect(0, 0, 200, 300));
        tableView.Source = new MyTableViewSource();
        View.AddSubview(tableView);
    }
}

public class MyTableViewSource : UITableViewSource
{
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(new NSString("Cell"));
        if (cell == null)
        {
            cell = new UITableViewCell(UITableViewCellStyle.Default, new NSString("Cell"));
        }

        cell.TextLabel.Text = "Item" + indexPath.Row;

        return cell;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return 10;
    }
}

最后我们可以通过调用PresentFromBarButtonItem将其显示在屏幕上。

【讨论】:

  • 您好,非常感谢您的回复!我是自学成才的 Xamarin 新手,所以如果我听起来很无知,请原谅我。我将如何“创建 [ing] 一个页面渲染器来显示 UIPopoverController”。我已按照此处的指南进行操作,并在使用他们提供的示例之前设法制作了自定义渲染器,但我无法弄清楚如何实现上述目标。只是想再次感谢您花时间写这个答案,我真的很感激:)
  • 当我说“这里”时,我的意思是添加这个链接developer.xamarin.com/guides/xamarin-forms/…
  • @Bracefor 如果你已经成功创建了一个自定义渲染器,你可以将我上面的代码放在渲染器中。当您在表单中导航到此页面时。可以看到想要达到的效果。
  • 好的,你能告诉我我应该在哪里保存哪些文件(文件名和应该在哪个文件夹中)只是为了确保我做对了吗?
  • @Bracefor 渲染器是在相应平台的项目中创建的。检查是否正确的一种方法是覆盖渲染器文件中的OnElementChanged,然后在此方法中设置断点。如果项目运行到这个方法,说明你已经成功制作了渲染器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
相关资源
最近更新 更多