Xamarin电子书阅读器页面导航的体系结构

时间:2017-04-03 13:45:34

标签: c# xamarin xamarin.forms

我开发了Xamarin电子书阅读器。我想问你关于页面导航架构的建议。我的应用程序包括主页面,其中包含书籍列表,页面包含特定书籍的章节,页面显示页面文本。

我的主页是继承自ContentPage

的自定义页面
/// <summary>
    /// The home page of the application.
    /// </summary>
    public class MainPage : ContentPage
    {
        private readonly StackLayout panel;

        /// <summary>
        /// The collection of books.
        /// </summary>
        private IEnumerable<EpubBook> books;

        /// <summary>
        /// Initialize an instance of the <see cref="MainPage"/>
        /// </summary>
        /// <param name="books">The collection of books.</param>
        public MainPage(IEnumerable<EpubBook> books)
        {
            this.Title = "Main page";
            this.books = books;
            this.panel = new StackLayout();

            foreach (EpubBook book in this.books)
            {
                OpenBookButton openBookButton = new OpenBookButton(book)
                {
                    Text = $"Title: {book.Title}"
                };

                openBookButton.Clicked += OnClickOpenBookButton;
                this.panel.Children.Add(openBookButton);
            }

            this.Content = new ScrollView
            {
                Content = this.panel,
                Orientation = ScrollOrientation.Vertical
            };
        }

        private async void OnClickOpenBookButton(object sender, EventArgs e)
        {
            OpenBookButton button = (OpenBookButton) sender;
            BookContentPage bookContentPage = new BookContentPage(button.Book.Chapters);
await this.Navigation.PushAsync(bookContentPage);
        }
    }

这是我的页面,显示了一本书的所有章节。

public class BookContentPage : ContentPage
    {
        private readonly StackLayout panel;

        public BookContentPage(List<EpubChapter> chapters)
        {
            this.Title = "Content page";
            this.panel = new StackLayout();

            foreach (EpubChapter chapter in chapters)
            {
                this.WriteChapter(chapter, panel);
            }

            this.Content = new ScrollView
            {
                Content = panel
            };
        }

        private void WriteChapter(EpubChapter epubChapter, Layout<View> layout)
        {
            // create buttons which have a text which displays the name of the chapter
        }

        private void OnClickOpenBookChapterButton(object sender, EventArgs args)
        {    
            OpenBookChapterButton button = (OpenBookChapterButton) sender;
            BookTextPage bookTextPage = new BookTextPage(button.Chapter);
            this.Navigation.PushAsync(bookTextPage);

            // I want to split bookTextPage to few little screen pages in the future

            this.Navigation.PushAsync(bookTextPage);
        }
    }

毕竟我想将书的文本拆分为页面并创建CarouselPage,其中包含所有这些页面。您对我开发此应用程序有何看法?我是Xamarin.Forms的新手。我是正确的吗?我做的很糟糕,我的方法应该修复什么?

1 个答案:

答案 0 :(得分:1)

很快就会弃用CarouselPage,因此您需要切换到CarouselView。使用此控件,页面架构将非常简单。

拥有一个ContentPage作为您拥有的图书清单。然后当您单击其中一个时,转到另一个内置CarouselView的ContentPage。这将绑定到您书中的页面列表,然后可以翻阅。