c# - Xamarin 电子书阅读器的页面导航架构

标签 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 的新手。我走的路对吗?我做错了什么,我的方法应该解决什么问题?

最佳答案

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

为您拥有的图书列表创建一个 ContentPage。然后,当您单击一个时,转到另一个 ContentPage,里面有一个 CarouselView。这将绑定(bind)到您书中的页面列表,然后他们可以翻阅。

关于c# - Xamarin 电子书阅读器的页面导航架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43186326/

相关文章:

c# - 当我的方法完成运行时,垃圾收集器会释放这个列表吗

xcode - Xamarin:错误 MT1108:找不到此 11.0.3 (15A432) 设备的开发人员工具

android - Xamarin Studio 2 - 最新的稳定更新 - 执行任务 Aapt : The source sequence is empty 时出错

xamarin - 为选取器设置背景图像

C#解构和重载

c# - 申请项目结构问题

ios - Xamarin Forms 自定义渲染器 ios

c# - 传递参数以点击手势 Xamarin 表单

c# - 如何设置日期时间选择器下拉列表以仅显示月份

xamarin - 1 属性未更新 Xamarin 表单绑定(bind)