【问题标题】:Is it possible to create a custom page layout with Xamarin.Forms?是否可以使用 Xamarin.Forms 创建自定义页面布局?
【发布时间】:2015-08-24 20:47:26
【问题描述】:

我想要实现的有点像这样

其中“某些视图”是一些自定义内容(页面或内容视图),它是固定的且永不更改(如状态栏),并且页面会通过正常导航(通过 NavigationPage)而更改。

我已经有类似的工作,但每次用户导航时都会重新创建“某些视图”,因为每个页面都源自包含“某些视图”的基本页面。

public sealed class RootPage : ExtendedMasterDetailPage
{
    public RootPage()
    {
        // 30%
        DrawerWidth = .3f;
        Master = new MenuPage();
        Detail = new NavigationPage(new HomePage());
    }
} 

public abstract class MasterPage : ContentPage
{
    private readonly Grid _root;
    private View _content;

    // Subclasses set this property to add their content
    public View Detail
    {
        get { return _content; }
        set
        {
            if (_content != null)
                _root.Children.Remove(_content);
            _content = value;
            if (_content != null)
                _root.Children.Add(_content, left: 0, top: 1);
            OnPropertyChanged();
        }
    }

    protected MasterPage()
    {
        _root = new Grid
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            RowDefinitions =
            {
                new RowDefinition {Height = new GridLength(60, GridUnitType.Absolute)},
                new RowDefinition {Height = new GridLength(1, GridUnitType.Star)},
            },
            RowSpacing = 0
        };

        // BuildStatusBar() creates the "static" content that is placed above the page
        _root.Children.Add(BuildStatusBar(), left: 0, top: 0);

        Content = _root;
    }
}

public sealed class HomePage : MasterPage
{
    public HomePage()
    {
        // Build content
        Detail = ...
    }
}

this blog post,作者做了一些与我类似的事情,但在 XAML 中,他似乎每次都重新加载他的内容;而我希望只有内容页面发生变化,并且标题“某些视图”保持在那里。

我相信我需要一些自定义渲染器,最好的文档似乎是this swipe to refresh custom renderer。但是我不知道如何将它应用到我的场景中。

【问题讨论】:

    标签: android xamarin.forms


    【解决方案1】:

    我认为您无法在 Xamarin.Forms 中实现这一目标。

    你可以用动画改变里面的内容视图的内容,但这不是正确的做法。 通过硬件键返回是不可能的(或太多的努力)。

    【讨论】:

    • 是的,那是很久以前的事了 :) 我们最终抓取了 Xamarin.Forms 并直接使用 Xamarin Android。我们用片段来实现它。
    • LOL 没看到这是 2014 年的问题:P