如何在WinRT XAML中向ListView添加多个Shapes.Path?

时间:2014-05-21 04:09:50

标签: c# xaml windows-runtime microsoft-metro winrt-xaml

我在XAML中有一个ListView控件,其中的项目由下面的代码(WinRT C#)设置。 但是我无法显示" path_2"看起来像下面的图像。

Image ListView with multiple Shapes.Path, path_2 NOT Appearing

如何在WinRT XAML中向ListView添加多个Shapes.Path?

[MainPage.xaml.cs中]

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;

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

            object o;
            Application.Current.Resources.TryGetValue("PathCustomStyle", out o);

            Path path_1 = new Path()
            {
                Style = (Style)o
            };

            Path path_2 = new Path()
            {
                Style = (Style)o
            };

            this.listView_path.Items.Add(path_1); // show
            this.listView_path.Items.Add(path_2); // doesn't show...
        }
    }
}

[App.xaml中]

<Application
    x:Class="AppListViewPath.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppListViewPath"
    RequestedTheme="Light">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="PathCustomStyle" TargetType="Path">
                <Setter Property="Data" Value="M95,15 C95,20 95,20 95,20"/>
                <Setter Property="Width" Value="200"/>
                <Setter Property="Height" Value="200"/>
                <Setter Property="Stroke" Value="Red"/>
                <Setter Property="Stretch" Value="Fill"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

[MainPage.xaml中]

<Page
    x:Class="AppListViewPath.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppListViewPath"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="listView_path"/>
    </Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

问题在于,您无法在可视化树中多次使用相同的几何体,并且样式在将其读入时将Path.Data引用解析为Geometry,而不是将字符串分配给目标并随后对其进行解析。由于几何图形已在使用中,因此第二个路径最终没有数据。

您可以为数据创建字符串资源,并将其与样式分开设置,而不是直接在样式中设置数据:

<ResourceDictionary>
    <Style x:Key="PathCustomStyle" TargetType="Path">
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="200"/>
        <Setter Property="Stroke" Value="Red"/>
        <Setter Property="StrokeThickness" Value="10" />
        <Setter Property="Stretch" Value="Fill"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <x:String x:Key="PathDataString">M95,15 C95,20 95,20 95,20</x:String>
<ResourceDictionary>

在Xaml中设置:

<Path Style="{StaticResource PathCustomStyle}" Data="{StaticResource PathDataString}" />

设置代码:

object o;
Application.Current.Resources.TryGetValue("PathCustomStyle", out o);

string stringData = (string)Application.Current.Resources["PathDataString"];

string xamlPath = "<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
"<Path.Data>" + stringData + "</Path.Data></Path>";

// Shows 10
for (int i = 0; i < 10; i++)
{
    Path path = XamlReader.Load(xamlPath) as Path;
    path.Style = (Style)o;

    this.listView_path.Items.Add(path);
}