Xamarin Forms - 子类化 MultiPage

Xamarin Forms - subclassing MultiPage

如何创建 Xamarin.Forms.MultiPage 的简单子类?

在下面的代码中,我有一个创建三个简单页面(每个页面上只有标签和按钮)的多页,然后将当前页面设置为定义的第一个页面。 但是调用 set CurrentPage 不会改变屏幕上显示的内容。显示的页面始终是我添加到 ItemsSource.

的最后一个页面

这是我的代码

using System;
using Xamarin.Forms;
using System.Diagnostics;
using System.Collections.Generic;

namespace NavigationTrial
{
    public class CustomMultiPage : MultiPage<Page>
    {
        public CustomMultiPage () : base()
        {
            var children = new List<Page>() { 
                new SimpleOnePage(), new SimpleTwoPage(), new SimpleThreePage() } ;

            this.ItemsSource = children;
            this.CurrentPage = this.Children[1];
        }

        override protected Page CreateDefault (object item) {
            return (Page)item ;
        }

    }
}

我想我遗漏了一些东西,但我找不到任何可以为我解决这个问题的例子。

当然可以,这就是多页的工作原理。

您所做的只是将布局堆叠在布局上,在 "currentpage" 都已经出现在屏幕上时更改哪个布局不会有太大作用。 如果您打算这样做,那么您将不得不手动管理它们的布局。

基本上,更改您不想看到的页面的布局,使它们不碍事。 像这样:

private Rectangle GetPageClosedRectangle()
{
    return new Rectangle(Width, 0, Width, Height);   
}

private Rectangle GetPageOpenedRectangle()
{
    return new Rectangle(0, 0, Width, Height);
}

然后随时换出页面 hide/show 一个:

PageIDontWantToSee.LayoutTo(getPageClosedRectangle());
PageIWantToSee.LayoutTo(getPageOpenedRectangle());

"LayoutTo" 方法是一种异步方法,它将布局动画设置为设置的矩形。如果你想要立即使用,你可以使用 page.Layout(Rectangle)

我还注意到设置 CurrentPage 对 MultiPage 没有影响(而它对 TabbedPage 有影响)。 因此,我将我想看到的 child 的 IsVisible 设置为 true,将其他 children 的 IsVisible 设置为 false。您甚至可以将其与 SelectedItem 和转换器绑定。

我发现它比之前的布局答案更简单。特别是对于 windows 桌面应用程序,您可以调整 window 的大小并查看 "hidden" 页面。