首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建数量可变的RichTextBoxOverflow元素

如何创建数量可变的RichTextBoxOverflow元素
EN

Stack Overflow用户
提问于 2017-12-12 23:46:53
回答 0查看 249关注 0票数 1

我正在千方百计地尝试跳出必须基于任意输入文本长度创建多个RichTextBlockOverflow控件的循环,但没有成功。HasOverflowContent属性既不同步也不异步更新。

变量bool "ThereIsText“我不能理解何时以及如何让它成为false来停止循环。

要粘贴到段落"Run“中的文本的链接是:text to paste

MainPage.xaml:

代码语言:javascript
复制
<Page
x:Class="Text_Viewer_Test_UWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Text_Viewer_Test_UWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid x:Name="Menù" HorizontalAlignment="Left" Width="290" Padding="0" Margin="0,21,0,0">
        <Grid Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>
            <Button  Grid.Row="0" x:Name="btnLoadText" Click="btnLoadText_Click" Content="Display text" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/>
            <TextBlock Grid.Row="1" x:Name="txtPage" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </Grid>
    <Grid x:Name="BaseGrid" Margin="320,10,30,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black">
        <ScrollViewer x:Name="PageViewer" Background="White" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible" VerticalScrollMode="Disabled" HorizontalScrollMode="Enabled">
            <StackPanel x:Name="StackViewer" VirtualizingStackPanel.VirtualizationMode="Recycling" Orientation="Horizontal"/>
        </ScrollViewer>
    </Grid>
</Grid>

MainPage.xaml.cs:

代码语言:javascript
复制
public sealed partial class MainPage : Page
{
    RichTextBlock TextOneRich = new RichTextBlock() { Margin = new Thickness(20) };
    List<RichTextBlockOverflow> TextList = new List<RichTextBlockOverflow>();
    bool ThereIsText = true;

    public MainPage()
    {
        this.InitializeComponent();

        StackViewer.Children.Add(TextOneRich);
        TextOneRich.Width = 400;
        TextOneRich.TextAlignment = TextAlignment.Justify;

    }

    private async void btnLoadText_Click(object sender, RoutedEventArgs e)
    {
        TextList.Clear();
        TextOneRich.Blocks.Clear();
        StackViewer.Children.Clear();
        StackViewer.Children.Add(TextOneRich);


        Paragraph paragraphText = new Paragraph();
        paragraphText.Inlines.Clear();
        paragraphText.Inlines.Add(new Run { Text = "PasteTextHere" });


        await Task.Run(async () =>
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                TextOneRich.Blocks.Add(paragraphText);
            });
        }).ContinueWith(async t =>
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                TextList.Add(new RichTextBlockOverflow() { Width = 400, Margin = new Thickness(20) });
                StackViewer.Children.Add(TextList[0]);
                TextOneRich.OverflowContentTarget = TextList[0];
            });
        });

        await Task.Run(async () =>
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
            {
                while (ThereIsText)
                {
                    await Task.Run(async () =>
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            TextList.Add(new RichTextBlockOverflow() { Width = 400, Margin = new Thickness(20) });
                            StackViewer.Children.Add(TextList[TextList.Count - 1]);
                            TextList[TextList.Count - 2].OverflowContentTarget = TextList[TextList.Count - 1];
                            txtPage.Text = TextList.Count.ToString();
                        });
                    });
                }
            });
        });
    }
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47776473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档