![](/img/trans.png)
[英]Xamarin.Forms on IOS: Remove space between SearchBar and NavigationBar
[英]Show “Back to Menu” Button in iOS NavigationBar with Xamarin.Forms
我正在尝试使用C#和Xamarin.Forms构建一个跨平台的应用程序。 它包含以MasterDetailPage
形式实现的滑出菜单。 在Android上,左上角有一个带有应用程序图标的按钮,可以切换滑出页面,iOS上没有这样的导航栏项目。
我将其分解为以下从Xamarin解决方案模板“Blank App(Xamarin.Forms Shared)”派生的最小示例,并替换App
-class的实现:
public class App
{
static MasterDetailPage MDPage;
public static Page GetMainPage()
{
return new NavigationPage(
MDPage = new MasterDetailPage {
Master = new ContentPage {
Title = "Master",
Content = new StackLayout {
Children = { Link("A"), Link("B"), Link("C") }
},
},
Detail = new ContentPage { Content = new Label { Text = "A" } },
});
}
static Button Link(string name)
{
var button = new Button { Text = name };
button.Clicked += delegate {
MDPage.Detail = new ContentPage { Content = new Label { Text = name } };
MDPage.IsPresented = false;
};
return button;
}
}
可以在GitHub上找到解决方案以及生成的屏幕截图。
我的想法是在iOS特定的代码中添加这样一个“菜单”或“后退”按钮,修改AppDelegate
类中的window.RootViewController.NavigationController.NavigationBar
。 但是window.RootViewController.NavigationController
为null
。
用NavigationPage
而不是Page
替换GetMainPage()
的返回类型没有帮助。
我可以通过添加工具栏项目MDPage.ToolbarItems.Add(...)
但它们出现在右上角 。
TL; DR
基本上,您的Detail
页面需要包含在NavigationPage
,以便后退按钮显示在iOS中。
这是我如何构建我的应用程序的示例。
App.cs
public static INavigation Navigation { get; set; }
public static Page GetMainPage(IContainer container)
{
return new MainPage();
}
MainPage.cs
public class MainPage : MasterDetailPage
{
public MainPage()
{
Title = "Some Title";
var master = new MainMenu();
var detail = new NavigationPage(new FirstPage());
if (App.Navigation == null)
{
App.Navigation = detail.Navigation;
}
Master = master;
Detail = detail;
}
}
既然你已经完成了这个,你的导航抽屉将按预期运行,你的ActionBar也会如此。
如果要在整个应用程序中导航,请使用静态定义的Navigation
await App.Navigation.PushAsync(new FooPage());
// or
await App.Navigation.PopAsync();
您在正确的轨道上,您的NavigatePage需要继续详细信息
Detail = new ContentPage { Content = new Label { Text = "A" } }
and
MDPage.Detail = new ContentPage { Content = new Label { Text = name } };
将会
Detail = new NavigationPage(new ContentPage { Content = new Label { Text = "A" } })
and
MDPage.Detail = new NavigationPage(new ContentPage { Content = new Label { Text = name } });
我终于找到了解决方案。 代码基本上需要两个小的修正:
DetailPage
包装在NavigationPage
,但不包含MasterDetailPage
(请参阅下面的#1,#2和#3)。 MasterPage
添加一个Icon
(参见下面的#4)。 不要忘记实际的PNG(!)到iOS资源。 最低工作示例如下:
public static class App
{
static MasterDetailPage MDPage;
public static Page GetMainPage()
{
return MDPage = new MasterDetailPage { // #1
Master = new ContentPage {
Title = "Master",
Icon = Device.OS == TargetPlatform.iOS ? "menu.png" : null, // #4
Content = new StackLayout {
Children = { Link("A"), Link("B"), Link("C") }
},
},
Detail = new NavigationPage(new ContentPage { Content = new Label { Text = "A" } }), // #2
};
}
static Button Link(string name)
{
var button = new Button { Text = name };
button.Clicked += delegate {
MDPage.Detail = new NavigationPage(new ContentPage { Content = new Label { Text = name } }); // #3
MDPage.IsPresented = false;
};
return button;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.