IOS上的工具栏或NavigationBar项目重复了自己为什么?

时间:2017-08-24 11:03:31

标签: c# ios xamarin.forms

我曾尝试在Xmarin.forms中实现工具栏项目,但在IOS上看到有线,保存并取消出现两次,为什么会发生这种情况。

以下是图片:

enter image description here

取消并保存已自行复制。

这是Xaml代码:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    Title="EA Mobile Directory"
    xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" x:Class="EAMobileDirectory.HomeActivity">
    <ContentPage.ToolbarItems>
    <ToolbarItem Text="Save"  Priority="0" Order="Primary" />
    <ToolbarItem Text="Cancel"  Priority="1" Order="Primary" />
</ContentPage.ToolbarItems>

然后我在 Project.IOS

中添加了一个Class
using System;
using System.Collections.Generic;
using EAMobileDirectory;
using EAMobileDirectory.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(HomeActivity), typeof(FridgeContentPageRenderer))]
namespace EAMobileDirectory.iOS
{
public class FridgeContentPageRenderer : PageRenderer
{
        public new HomeActivity Element
    {
            get { return (HomeActivity)base.Element; }
    }

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

        var LeftNavList = new List<UIBarButtonItem>();
        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;

            if (ItemPriority == 1)
            {
                UIBarButtonItem LeftNavItems = navigationItem.RightBarButtonItems[i];
                LeftNavList.Add(LeftNavItems);
            }
            else if (ItemPriority == 0)
            {
                UIBarButtonItem RightNavItems = navigationItem.RightBarButtonItems[i];
                rightNavList.Add(RightNavItems);
            }
        }

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

    } }
}

为什么会发生这种情况? ,请帮忙。

修改

enter image description here

这是App.cs:

使用Xamarin.Forms;

命名空间EAMobileDirectory {     公共部分类App:应用程序     {         static DataProvider dbUtils;

        public App()
        {
            InitializeComponent();


            try
            {
                DataProvider db = new DataProvider();
                string country = db.GetSelectedCountry();
                if (country != null)
                {

                    MainPage = new NavigationPage(new MainActivity());

                }

                else
                {


                    MainPage = new NavigationPage(new SetCountry())

                    {
                        //BarBackgroundColor = Color.FromHex("#FF0000"),
                        BarTextColor = Color.White,
                        Title = "Select your country",



                    };
                }
            }
            catch (System.NullReferenceException ex)
            {
                MainPage = new NavigationPage(new SetCountry())

                {
                    //BarBackgroundColor = Color.FromHex("#FF0000"),
                    BarTextColor = Color.White,
                    Title = "Select your country",



                };

            }

            catch (System.Exception e)
            {
                MainPage = new NavigationPage(new SetCountry())

                {
                    //BarBackgroundColor = Color.FromHex("#FF0000"),
                    BarTextColor = Color.White,
                    Title = "Select your country",



                };
            }

        }

        public static DataProvider DAUtil
        {
            get
            {
                if (dbUtils == null)
                {
                    dbUtils = new DataProvider();
                }
                return dbUtils;
            }
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

0 个答案:

没有答案