C# 如何在WinRT XAML中将多个shape.Path添加到ListView?
我在XAML中有一个ListView控件,它的项由下面的代码(WinRT C#)设置样式。 但我无法显示如下图所示的“路径2” 如何在WinRT XAML中将多个shape.Path添加到ListView [MainPage.xaml.cs]C# 如何在WinRT XAML中将多个shape.Path添加到ListView?,c#,xaml,windows-runtime,microsoft-metro,winrt-xaml,C#,Xaml,Windows Runtime,Microsoft Metro,Winrt Xaml,我在XAML中有一个ListView控件,它的项由下面的代码(WinRT C#)设置样式。 但我无法显示如下图所示的“路径2” 如何在WinRT XAML中将多个shape.Path添加到ListView [MainPage.xaml.cs] using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Shapes; namespace AppListViewPath { public se
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>
[主页.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>
问题在于,您不能在可视化树中多次使用同一几何体,并且样式会在读取几何体时解析路径。数据引用几何体,而不是将字符串指定给目标并随后解析。由于几何图形已在使用中,因此第二条路径最终没有数据 不必直接在样式中设置数据,您可以为数据创建字符串资源,并设置与样式分离的字符串:
<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>
M95,15 C95,20 95,20 95,20
要在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);
}
对象o;
Application.Current.Resources.TryGetValue(“PathCustomStyle”,out o);
string stringData=(string)Application.Current.Resources[“PathDataString”];
字符串xamlPath=“”+
“+stringData+”;
//显示10
对于(int i=0;i<10;i++)
{
Path Path=XamlReader.Load(xamlPath)作为路径;
Style=(Style)o;
this.listView\u path.Items.Add(path);
}