Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF为每个文本块拆分HTML字符串_Html_Wpf_String_Split_Textblock - Fatal编程技术网

WPF为每个文本块拆分HTML字符串

WPF为每个文本块拆分HTML字符串,html,wpf,string,split,textblock,Html,Wpf,String,Split,Textblock,有人知道如何分割每个文本块的HTML字符串吗?我把它们分开了,但有点不对劲。每一页的行数不尽相同。我如何解决这个问题 代码如下: XAML: 代码: 使用系统; 使用System.Collections.Generic; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Media; 利用制度全球化; 使用System.IO; 命名空间电子书 { 公共部分类主窗口:窗口 { 公共主窗口() { 初始化组

有人知道如何分割每个文本块的HTML字符串吗?我把它们分开了,但有点不对劲。每一页的行数不尽相同。我如何解决这个问题

代码如下:

XAML:


代码:

使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
利用制度全球化;
使用System.IO;
命名空间电子书
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
字符串html_str=“自1500年代以来使用的标准Lorem Ipsum段落”
+ "“Lorem ipsum door sit amet,adipiscing elit,sed do eiusmod temporal incident ut labour and dolore magna aliqua.但是,在最小限度的情况下,我们不需要在公共消费品上进行任何实践。两人或两人或两人在无法定财产的情况下代表不同的权利。例外。”圣奥卡塔人不自以为是,必须为自己的劳动付出代价。

” +“西塞罗在公元前45年写的《博诺伦和马洛伦的终结》第1.10.32节” + "所有人都会犯错误,但要清楚地理解错误,就必须承认错误,承认错误,承认错误,承认正确的发明家和准建筑师的观点,并解释错误。我们必须承认错误,承认错误,承认错误,承认错误,承认错误,承认错误,承认错误,承认错误第三种情况是,在劳动和就业领域内,非临时性的劳动和就业是平等的,但在最低限度内,是由劳动和就业领域内的非临时性的非临时性的劳动和就业“在一个没有法律约束的环境中,我们是否需要保护自己的权利,在一个没有法律约束的环境中保护自己的权利?”

” +“1914年由H.Rackham翻译” + "“但我必须向你们解释,谴责快乐、赞美痛苦的错误观念是如何产生的,我将给你们一个完整的系统描述,并阐述伟大的真理探索者、人类幸福的建设者的实际教导。没有人拒绝、不喜欢或回避快乐本身,因为快乐本身就是快乐,但是因为那些不知道如何理性地追求快乐的人会遇到极其痛苦的后果。同样,也没有人爱、追求或渴望获得痛苦本身,因为痛苦本身就是痛苦,而是因为偶尔会发生一些情况,在这些情况下,辛劳和痛苦可以使他获得一些巨大的快乐e、 除了从中获得一些好处外,我们中谁曾进行过艰苦的体育锻炼?但是,谁有权对一个选择享受没有烦人后果的快乐的人,或者一个选择避免不会产生快乐的痛苦的人挑剔呢?“

” +“西塞罗在公元前45年写的《博诺伦和马洛伦的终结》第1.10.33节” + "“在最高法院和最高法院,除了在法院的审判之外,其他人都有腐败行为,同样的罪过也必须是出于对动物的同情、对劳动的同情和对他人的同情,以及对他人的同情和对他人的尊重。”暂时性自由,以及解决方案,不妨碍现状负身份,最大限度地在有身份的情况下,假设所有人都有权利,所有人都有权拒绝。暂时性自由和官方自由的必要性和必要性,即使没有必要,也不会导致不复发性疾病的发生。Itaqu我们的原则是一种智慧的缺失,但我们必须认识到这一点,我们必须认识到这一点 +“1914年由H.Rackham翻译” + "“另一方面,我们义愤填膺地谴责和厌恶那些被当下快乐的魅力所迷惑、意志消沉、被欲望所蒙蔽、无法预见必然会发生的痛苦和麻烦的人;而同样的责任属于那些由于意志薄弱而失职的人,这也是一个错误在一个自由的时刻,当我们的选择权不受限制,当没有任何事情阻止我们做我们最喜欢做的事情时,每一种快乐都是受欢迎的,每一种痛苦都是可以避免的。但是在某些情况下,由于责任或义务的要求,我们可以避免这种情况商业的义务经常发生的事情是,快乐必须被拒绝,烦恼必须被接受。因此,智者在这些事情上总是坚持这个选择原则:他拒绝快乐以获得其他更大的快乐,否则他忍受痛苦以避免更大的痛苦。

”; private void AutoNextClick(对象发送方,路由目标) { myBook.AnimateToNextPage(假,700); myBook.Focus(); } private void AutoPreviousClick(对象发送者,路由目标e) { 我的书
<Window x:Class="Ebook.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFMitsuControls;assembly=WPFMitsuControls"
Title="eBook" Height="600" Width="800"
Loaded="MainWindow_OnLoaded" Background="Silver">
    <DockPanel>
        <Viewbox Margin="10">
            <Grid>
                <controls:Book x:Name="myBook" Width="600" Height="400" Margin="20">
                    <controls:Book.ItemTemplate>
                        <DataTemplate>
                            <Border BorderThickness="4" BorderBrush="Gray" Background="White">
                                <ContentControl Content="{Binding .}" />
                            </Border>
                        </DataTemplate>
                    </controls:Book.ItemTemplate>
                </controls:Book>
                <Button Content="&lt;" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent" Height="50" Click="AutoPreviousClick" />
                <Button Content="&gt;" HorizontalAlignment="Right" VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent" Height="50" Click="AutoNextClick" />
            </Grid>
        </Viewbox>
    </DockPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
using System.IO;

namespace Ebook
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        string html_str =  "<p>The standard <b>Lorem Ipsum</b> passage, used since the 1500s</p>"
                        + "<p>'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</p>"
                        + "<p>Section 1.10.32 of 'de Finibus Bonorum et Malorum', written by Cicero in 45 BC</p>"
                        + "<p>'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?'</p>"
                        + "<p><u>1914 translation by H. Rackham</u></p>"
                        + "<p>'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?'</p>"
                        + "<p>Section 1.10.33 of 'de Finibus Bonorum et Malorum', written by Cicero in 45 BC</p>"
                        + "<p>'At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.'</p>"
                        + "<p><i>1914 translation by H. Rackham</i></p>"
                        + "<p>'On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.'</p>";



        private void AutoNextClick(object sender, RoutedEventArgs e)
        {
            myBook.AnimateToNextPage(false, 700);
            myBook.Focus();
        }

        private void AutoPreviousClick(object sender, RoutedEventArgs e)
        {
            myBook.AnimateToPreviousPage(false, 700);
            myBook.Focus();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            List<string> lines = WrapText(html_str, 200, "Arial", 12); 
            int rows = 20;
            int pages = lines.Count / rows;
            string _Page = "";
            int j = 0, k = 0;
            TextBlock tb1;
            for (int i = 0; i < pages; i++)
            {
                tb1 = new TextBlock();
                tb1.TextWrapping = TextWrapping.Wrap;
                tb1.Margin = new Thickness(5);
                tb1.TextAlignment = TextAlignment.Justify;
                _Page = "";
                for (j = 0; j < rows; j++)
                {
                    _Page += lines[rows * i + j] + "\n";
                    k++;
                }
                //myBook.Items.Add(new TextBlock { Text = _Page, TextWrapping = TextWrapping.Wrap, Margin = new Thickness(5), TextAlignment = TextAlignment.Justify });
                tb1.Inlines.AddRange(MarkupProcessor.HTMLToWPF(_Page));
                myBook.Items.Add(tb1);
            }
        }

        static List<string> WrapText(string text, double pixels, string fontFamily, float emSize)
        {
            string[] originalLines = text.Split(new string[] { " " },
                StringSplitOptions.None);

            List<string> wrappedLines = new List<string>();

            StringBuilder actualLine = new StringBuilder();
            double actualWidth = 0;

            foreach (var item in originalLines)
            {
                FormattedText formatted = new FormattedText(item,
                    CultureInfo.CurrentCulture,
                    System.Windows.FlowDirection.LeftToRight,
                    new Typeface(fontFamily), emSize, Brushes.Black);

                actualLine.Append(item + " ");
                actualWidth += formatted.Width;

                if (actualWidth > pixels)
                {
                    wrappedLines.Add(actualLine.ToString());
                    actualLine = new StringBuilder();
                    actualWidth = 0;
                }
            }

            if (actualLine.Length > 0)
                wrappedLines.Add(actualLine.ToString());

            return wrappedLines;
        }
    }
}
tb1.TextWrapping = TextWrapping.Wrap;