سؤال

The following program adds 2000 Line objects to a Canvas when the button is pressed:

MainPage.xaml:

<Page
    x:Class="ManyLinesTestCs.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ManyLinesTestCs"
    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}">
        <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="630" Margin="266,10,0,0" VerticalAlignment="Top" Width="1090"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="7,7,0,0" VerticalAlignment="Top"/>

    </Grid>
</Page>

MainPage.xaml.cs:

using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.UI;

namespace ManyLinesTestCs
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            var random = new Random();

            button.Click += (s, e) =>
                {
                    foreach (var elt in Enumerable.Range(0, 2000))
                        canvas.Children.Add(
                            new Line() 
                            {
                                Stroke = new SolidColorBrush(Colors.SteelBlue),
                                StrokeThickness = 0.1,
                                X1 = random.Next((int)canvas.Width),
                                Y1 = random.Next((int)canvas.Width),
                                X2 = random.Next((int)canvas.Width),
                                Y2 = random.Next((int)canvas.Width)
                            });
                };
        }
    }
}

On my system, after pressing the button, the screen fills with the start screen background color.

If I change the count to 1500 or so, the lines display as expected.

I realize that shapes are somewhat heavyweight and are not intended to be drawn in this quantity.

What sort of limit is being applied here?

In WPF, a lightweight alternative to full-on Line shapes it to use DrawingContext.DrawLine. Is there a similar alternative in Winrt?

هل كانت مفيدة؟

المحلول

If you are asking for academic reasons, I am not sure the answer is available and I would expect it to have a dependency on hardware. But if you are asking because you need to draw a complex object with many segments like this, I would suggest you use Path - which can contain many disconnected segments like those in your experiment above.

نصائح أخرى

Here's a variation of the above MainPage.xaml.cs which uses a GeometryGroup to contain many LineGeometry objects, as Jerry Nixon suggested. It indeed allows for many thousands of line segments without crashing.

using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.UI;
using Windows.Foundation;

namespace ManyLinesTestCs
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            var geometryGroup = new GeometryGroup();

            canvas.Children.Add(
                new Path()
                {
                    Stroke = new SolidColorBrush(Colors.SteelBlue),
                    StrokeThickness = 1.0,
                    Data = geometryGroup
                });

            var random = new Random();

            button.Click += (s, e) =>
                {
                    foreach (var elt in Enumerable.Range(0, 2000))
                        geometryGroup.Children.Add(
                            new LineGeometry()
                            {
                                StartPoint = new Point(random.Next((int)canvas.Width), random.Next((int)canvas.Width)),
                                EndPoint = new Point(random.Next((int)canvas.Width), random.Next((int)canvas.Width))
                            });
                };
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top