什么是形状限制?

时间:2014-05-06 03:38:41

标签: windows-store-apps winrt-xaml

以下程序在按下按钮时将Line个对象添加到Canvas

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)
                            });
                };
        }
    }
}

在我的系统上,按下按钮后,屏幕将显示开始屏幕背景颜色。

如果我将计数更改为1500左右,则会按预期显示行。

我意识到形状有点重量级,并不打算以这个数量绘制。

这里适用什么样的限制?

在WPF中,全开Line的轻量级替代形式使其成形为使用DrawingContext.DrawLine。在Winrt中有类似的替代方案吗?

2 个答案:

答案 0 :(得分:1)

如果您要求学术原因,我不确定答案是否可用,我希望它能依赖硬件。但是如果你问,因为你需要绘制一个包含许多这样的段的复杂对象,我建议你使用Path - 它可以包含许多断开的段,就像你上面的实验那样。

答案 1 :(得分:1)

以上是MainPage.xaml.cs的变体,它使用GeometryGroup来包含许多LineGeometry个对象,正如Jerry Nixon所说。它确实允许数千个线段而不会崩溃。

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))
                            });
                };
        }
    }
}