8

我正在尝试使用 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 的代码中添加这样的“菜单”或“后退”按钮来修改类window.RootViewController.NavigationController.NavigationBar中的AppDelegate。但是window.RootViewController.NavigationControllernull

GetMainPage()替换by的返回类型NavigationPage而不是Page没有帮助。

我可以通过添加工具栏项MDPage.ToolbarItems.Add(...),但它们出现在右上角

4

3 回答 3

5

TL;博士

本质上,您的Detail页面需要包含在 aNavigationPage中,以便后退按钮出现在 iOS 中。


这是我如何构建应用程序的示例。

应用程序.cs

    public static INavigation Navigation { get; set; }

    public static Page GetMainPage(IContainer container)
    {
        return new 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;
    }
}

现在您已完成此操作,您的 Navigation Drawer 将按预期运行,您的 ActionBar 也将如此。

当您想在整个应用程序中导航时,您可以使用静态定义的Navigation

await App.Navigation.PushAsync(new FooPage());
// or
await App.Navigation.PopAsync();
于 2014-07-15T20:05:27.663 回答
1

你在正确的轨道上,你的 NavigatePage 需要继续 Detail 所以

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 } });
于 2014-07-12T20:37:25.763 回答
1

我终于找到了解决方案。代码基本上需要两个小修正:

  1. DetailPage将所有s包装在 a 中NavigationPage,而不是 the MasterDetailPage(参见下面的 #1、#2 和 #3)。
  2. 在 iOS 上添加一个 when(见下面Icon的# 4)。MasterPage不要忘记将实际的 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;
    }
}
于 2016-01-19T14:08:56.423 回答