Android只允许一个导航页面——Xamarin表单
您好,我使用Xamarin.Forms创建了这个应用程序——它在iOS中运行良好,但在Android中肯定不起作用,并且面临着标题中提到的一些问题 Lemme解释我的应用程序和代码继续,该应用程序有一个登录屏幕,并将在这个刚刚填充的列表视图及其单元格中使用导航转到名为HomeController的主屏幕。当点击每一行时,就会进入到主控详细信息页面,我得到了一个错误,它在iOS而不是Android中运行良好 App.cs LoginController.cs HomeController.cs LeadViewController.cs:MasterDetailScreen:面向屏幕的错误Android只允许一个导航页面——Xamarin表单,android,xamarin.forms,Android,Xamarin.forms,您好,我使用Xamarin.Forms创建了这个应用程序——它在iOS中运行良好,但在Android中肯定不起作用,并且面临着标题中提到的一些问题 Lemme解释我的应用程序和代码继续,该应用程序有一个登录屏幕,并将在这个刚刚填充的列表视图及其单元格中使用导航转到名为HomeController的主屏幕。当点击每一行时,就会进入到主控详细信息页面,我得到了一个错误,它在iOS而不是Android中运行良好 App.cs LoginController.cs HomeController.cs L
尝试以下代码:
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;
};
您的根页面是导航页面。您还添加了一个带有导航页面的Masterdedial页面 无论是MasterDetail还是Navigation都应该是根目录,而不是将它们放在彼此内部。每个页面中不能有2个导航页面 您可以将导航页面作为MasterDetail中的详细信息,也可以在导航页面中包含MasterDetail,但在任何级别的导航页面中都不能包含导航页面。这是一个仅限Android的限制,但最终成为Xamarin表单限制。 这是您的错误代码:
Detail=newnavigationpage(newletsgetstartedpage())代码>
我认为您可以尝试以下代码:
Detail=newletsgetstartedpage()代码>
或
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;
}
}
}
请确保您的App.cs只有这样的设置页面
MainPage=newmastertestpage()强>
不要这样设置
新建导航页面(新建MasterTestPage())强>
MasterTestPage是我的MasterDetailPage类请让我知道为什么我在安卓系统而不是iOS系统中面临问题。如何重现此问题。您能显示一些错误日志吗?我将尝试@Jay提到的以下代码,如果没有帮助,我将发送一些错误日志-迈克,谢谢。事实上,我更新了Xamarin Studio所需的Xcode 8.2,因此无法构建应用程序,因此延迟了。这只是重新表述了另一个答案,但没有太多细节
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 ();
}
}
}
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;
}
}
}
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;
};
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;
}
}
}