Android只允许一个导航页面 - Xamarin Forms

时间:2016-12-26 12:45:03

标签: android xamarin.forms

您好我使用Xamarin.Forms创建了该应用程序 - 它在iOS中运行良好但当然它不适用于Android并且面临标题中提到的一些问题。

Lemme解释我的应用程序和代码继续,该应用程序有一个登录屏幕,并将在这个刚填充的列表视图及其单元格中使用导航移动到名为HomeController的主屏幕。虽然点击每一行会转到Master详细信息页面,但我收到错误,它在iOS而不是Android中正常工作。

  

App.cs

using System;
using Xamarin.Forms;

namespace eMO_Xamarin
{
public class App : Application
{
public App ()
{
// The root page of your application

var nav = new NavigationPage (new LoginViewController ());
nav.BarBackgroundColor = Color.FromHex("#EEEEEE");
nav.BarTextColor = Color.FromHex("#424242");

MainPage = nav;
}

}
}
  

LoginController.cs

using System;

using Xamarin.Forms;

using System.Collections.Generic;


namespace eMO_Xamarin
{
public class LoginViewController : ContentPage
{

Entry userEntry, passwordEntry;

public LoginViewController ()
{

NavigationPage.SetBackButtonTitle (this, "");
NavigationPage.SetHasBackButton (this, false);
this.BackgroundImage = "Bg1.jpg";

userEntry = new Entry () {

HorizontalOptions = LayoutOptions.FillAndExpand,
Placeholder = "Username"
};

passwordEntry = new Entry () {

HorizontalOptions = LayoutOptions.FillAndExpand,
Placeholder = "Password",
IsPassword = true
};

var loginButton = new Button () {

Text = "Login",
TextColor = Color.White,
BackgroundColor = Color.FromHex ("77D065")

};
loginButton.Clicked += OnButtonClickedLogin;

Title = "e-Loan";

this.Padding = new Thickness (50, Device.OnPlatform (20, 0, 0), 50, 20);

stack.Children.Add (appLogoImg);
stack.Children.Add (userEntry);
stack.Children.Add (passwordEntry);
stack.Children.Add (loginButton);

this.Content = scroll;

}


void OnButtonClickedLogin (object sender, EventArgs e)
{

Navigation.InsertPageBefore (new HomeViewController (), this);
Navigation.PopAsync ();

}

}
}
  

HomeController.cs

using System;

using System.Collections.Generic;

using Xamarin.Forms;


namespace eMO_Xamarin
{
    public class HomeViewController : ContentPage
    {
        public HomeViewController ()
        {

            Title = "Welcome John!";

            NavigationPage.SetBackButtonTitle (this, "Back");
            NavigationPage.SetHasBackButton (this, false);

            if (Device.Idiom == TargetIdiom.Phone) {

                this.BackgroundImage = "login_home.jpg";

            } else if (Device.Idiom == TargetIdiom.Tablet) {

                this.BackgroundImage = "Bg6.jpg";

            } else {

            }

            var toolbarItem = new ToolbarItem {
                Text = "Logout"
            };
            toolbarItem.Clicked += OnLogoutButtonClicked;
            ToolbarItems.Add (toolbarItem);

            Label header = new Label {
                Text = "Submitted Loans",
                TextColor = Color.Gray,
                FontAttributes = FontAttributes.Bold,
                FontSize = 30,
                HorizontalOptions = LayoutOptions.Center
            };

            // Create a data template from the type ImageCell
            var cell = new DataTemplate (typeof(MenuCell));

            ListView listView = new ListView {

                ItemsSource = VetData.GetData (),
                ItemTemplate = cell, // Set the ImageCell to the item template for the listview
                //SeparatorColor = Color.Transparent

            };

            listView.RowHeight = 75;
            listView.BackgroundColor = Color.Transparent;

            // Push the list view down below the status bar on iOS.
            if (Device.Idiom == TargetIdiom.Phone) {

                Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 0, 0);

            } else if (Device.Idiom == TargetIdiom.Tablet) {

                Padding = new Thickness (150, Device.OnPlatform (50, 0, 0), 150, 10);

            } else {

            }

            // Set the content for the page.

            this.Content = new StackLayout {

                Spacing = 20,

                Children = {
                    header,
                    listView
                }
            };

            listView.ItemSelected += async (sender, e) => {


                if (e.SelectedItem != null) {
                    //do what you want with the selectedItem
                    // Navigation with back push
                    await Navigation.PushAsync (new LeadViewController ());

                }

                //then init the selectedItem of the listview to enable it to be selected again
                listView.SelectedItem = null;
            };

        }

        async void OnLogoutButtonClicked (object sender, EventArgs e)
        {
            // Navigation with out back push
            Navigation.InsertPageBefore (new LoginViewController (), this);
            await Navigation.PopAsync ();
        }


    }
}
  

LeadViewController.cs:MasterDetailScreen:屏幕面临错误

using System;

    using System.Collections.Generic;

    using Xamarin.Forms;

    namespace eMO_Xamarin
    {
        public class LeadViewController : MasterDetailPage
        {
            public LeadViewController ()
            {
                this.BackgroundImage = "Bg6.jpg";

                var menuPage = new MenuPage ();
                menuPage.OnMenuSelect = (categoryPage) => {

                Detail = new NavigationPage (categoryPage);

                    if (Device.Idiom == TargetIdiom.Phone) {

                        IsPresented = false;

                    } else if (Device.Idiom == TargetIdiom.Tablet) {

                        IsPresented = true;

                    } else {

                        IsPresented = true;
                    }
                };

                Master = menuPage;

                Detail = new NavigationPage (new LetsGetStartedPage ());

                MasterBehavior = MasterBehavior.Default;

            }

        }
    }

2 个答案:

答案 0 :(得分:1)

您的根页是导航页面。您还添加了一个带有导航页面的MasterDetial页面。

The similar issue here

  

MasterDetail或Navigation应该是根,而不是彼此在一起。您不能在其他

中包含2个导航页面      

您可以将导航页面作为MasterDetail中的详细信息,或者您可以在导航页面中拥有MasterDetail,但您无法在导航页面内的任何级别拥有导航页面。这是仅限Android的限制,但最终会使其成为Xamarin Forms限制。

这是您的错误代码:

Detail = new NavigationPage (new LetsGetStartedPage ());

我认为您可以尝试以下代码:

Detail = new LetsGetStartedPage ();

namespace eMO_Xamarin
{
   public class App : Application
   {
      public App ()
      {
         var nav = new LoginViewController ();
         nav.BarBackgroundColor = Color.FromHex("#EEEEEE");
         nav.BarTextColor = Color.FromHex("#424242");    
         MainPage = nav;
      }

   }
}

答案 1 :(得分:0)

试试这段代码:

listView.ItemSelected += async (sender, e) => {
    if (e.SelectedItem != null) {
        //do what you want with the selectedItem
        // Navigation with back push
        await Navigation.PushAsync (new NavigationPage(new LeadViewController ()));
    }
    //then init the selectedItem of the listview to enable it to be selected again
    listView.SelectedItem = null;
};