Xamarin表单 - CustomRenderer在导航后不起作用

问题描述 投票:3回答:1

我的主页面的iOS Native代码中有一个自定义渲染器。应用程序启动时它完美正常,并使用我想要的iOS系统图标呈现Navbar项目。但是,如果我离开主页面,当我向后导航时,RightBarButtonItems数组只包含两个未实例化的对象,我会检查(RightNavItems.Title == null)以便继续这种情况,看看会发生什么,实际上项目是没有呈现,如果我离开并再次返回应用程序崩溃,因为RightBarButtonItems数组现在是空的。

为什么导航回主页时工具栏项是未初始化的?在这样的自定义渲染器中处理导航的正确方法是什么?

以下是自定义渲染器的代码:

public class ItemsPageRenderer : PageRenderer
{
    public new ItemsPage Element
    {
        get { return (ItemsPage)base.Element; }
    }

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    var rightNavList = new List<UIBarButtonItem>();

    var navigationItem = this.NavigationController.TopViewController.NavigationItem;

    for (var i = 0; i < Element.ToolbarItems.Count; i++)
    {
        var reorder = (Element.ToolbarItems.Count - 1);
        var ItemPriority = Element.ToolbarItems[reorder - i].Priority;

        UIBarButtonItem RightNavItems = navigationItem.RightBarButtonItems[i];
        if (RightNavItems.Title == null)
            continue;

        if (RightNavItems.Title.ToLower() == "add")
        {
            rightNavList.Add(new UIBarButtonItem(UIBarButtonSystemItem.Add)
            {
                Action = RightNavItems.Action,
                Target = RightNavItems.Target
            });
        }
        else if (RightNavItems.Title.ToLower() == "edit")
        {
            rightNavList.Add(new UIBarButtonItem(UIBarButtonSystemItem.Edit)
            {
                Action = RightNavItems.Action,
                Target = RightNavItems.Target
            });
        }
        else
        {
            rightNavList.Add(RightNavItems);
        }
    }

    navigationItem.SetRightBarButtonItems(rightNavList.ToArray(), false);
}
}
c# ios xamarin xamarin.forms xamarin.ios
1个回答
1
投票

检查此代码段

var rightNavList = new List<UIBarButtonItem>();
var navigationItem = this.NavigationController.TopViewController.NavigationItem;
for (var i = 0; i < Element.ToolbarItems.Count; i++)
{
    var reorder = (Element.ToolbarItems.Count - 1);
    var ItemPriority = Element.ToolbarItems[reorder - i].Priority;

    UIBarButtonItem RightNavItems = navigationItem.RightBarButtonItems[i];
    if (RightNavItems.Title == null)
        continue;

    if (RightNavItems.Title.ToLower() == "add")
    {
        rightNavList.Add(new UIBarButtonItem(UIBarButtonSystemItem.Add)
        {
            Action = RightNavItems.Action,
            Target = RightNavItems.Target
        });
    }
    else if (RightNavItems.Title.ToLower() == "edit")
    {
        rightNavList.Add(new UIBarButtonItem(UIBarButtonSystemItem.Edit)
        {
            Action = RightNavItems.Action,
            Target = RightNavItems.Target
        });
    }
    else
    {
        rightNavList.Add(RightNavItems);
    }
}

navigationItem.SetRightBarButtonItems(rightNavList.ToArray(), false);

您在第一次进入页面时将项目外观从title更改为icon,但是,当第二次进入页面时,条件RightNavItems.Title == null为true,因此它跳出循环而不向列表中添加任何项目,因此navigationItem.SetRightBarButtonItems在以下处添加空数组持续。

Solution

修改如下

if (RightNavItems.Title == null)
{
      rightNavList.Add(RightNavItems); //add this line.
      continue;
}
© www.soinside.com 2019 - 2024. All rights reserved.