如何在Silverlight中克隆路径资源? [英] How do you clone a path resource in Silverlight?

查看:88
本文介绍了如何在Silverlight中克隆路径资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xaml文件中定义了一个资源,如下所示:

 <Path x:Key="myPath"
    Data="M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687 10.239936,27.573483
    L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
    C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
    C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
    C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
    27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
    24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
    C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
    2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
    14.879907,11.786915 z"
 />

我希望能够将此路径的多个实例"(以及其他几个)添加到StackPanel.当然,我不能简单地将"myPath"添加到面板中,因为它已经是另一个容器的子代了.

但是,我似乎也无法克隆路径.我尝试过:

Path clone = new Path() 
{
  Data = source.Data
};

但是没有运气...关于价值超出预期范围的例外.

最后,我尝试挖掘source.Data(一个PathGeometry),但其中不包含PathFigures ...我也不知道为什么,因为如果将PathPath直接从资源部分复制到面板中,Path会渲染. /p>

有什么作用?

谢谢, 塞尔吉奥

解决方案

将路径数据放入字符串资源:

<Page.Resources>
    <system:String x:Key="PathData">
    M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687  10.239936,27.573483
    L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
    C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
    C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
    C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
    27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
    24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
    C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
    2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
    14.879907,11.786915 z
    </system:String>
</Page.Resources>

并使用它:

<Path x:Name="Path1" Data="{StaticResource PathData}" Fill="Blue" ... />
<Path x:Name="Path2" Data="{StaticResource PathData}" Fill="Red" ... />

您将在XAML声明中使用它:

xmlns:system="clr-namespace:System;assembly=mscorlib"

如果要使用公共路径字符串以编程方式创建路径,Silverlight将缺少WPF的一些关键功能-因此您必须对其加以混淆:

string pathXaml = 
@"<Path xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
        xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
        Data=""path_data_goes_here"" />";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);

I have a resource defined in my Xaml file as follows:

 <Path x:Key="myPath"
    Data="M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687 10.239936,27.573483
    L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
    C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
    C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
    C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
    27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
    24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
    C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
    2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
    14.879907,11.786915 z"
 />

I want to able to add multiple "instances" of this path (and several others) to a StackPanel. Of course, I can't simply add "myPath" to the panel since it's already a child of another container.

However, I can't seem to be able to clone the path either. I've tried:

Path clone = new Path() 
{
  Data = source.Data
};

But no luck...exception about value being out of expected range.

Finally, I tried digging into source.Data (a PathGeometry), but it contains no PathFigures...I have no idea why, since the Path does render if I copy it from the resource section to a panel directly.

What gives?

Thanks, Sergio

解决方案

Put the path data in a string resource:

<Page.Resources>
    <system:String x:Key="PathData">
    M14.773241,18.080208 C12.373256,18.080208 10.239936,19.30687  10.239936,27.573483
    L10.239936,36.106766 C10.239936,45.440037 12.586588,46.506699 14.986573,46.506699
    C18.613216,46.506699 19.359879,42.400059 19.359879,35.3601 L19.359879,27.733482
    C19.359879,20.05353 17.386559,18.080208 14.773241,18.080208 z M14.879907,11.786915
    C17.973221,11.786915 22.293194,13.013573 24.906511,17.920212 C26.773167,21.386856
    27.519829,27.093487 27.519829,32.213455 C27.519829,34.506775 27.306496,41.706726
    24.906511,46.453365 C23.626518,49.013351 20.906536,52.799992 15.199905,52.799992
    C2.1333201,52.799992 2.1333201,37.600086 2.1333201,32.160122 C2.1333201,28.05348
    2.1333201,22.666847 4.4266391,18.453541 C5.8666301,15.840225 8.639946,11.786915
    14.879907,11.786915 z
    </system:String>
</Page.Resources>

And to use it:

<Path x:Name="Path1" Data="{StaticResource PathData}" Fill="Blue" ... />
<Path x:Name="Path2" Data="{StaticResource PathData}" Fill="Red" ... />

You'll need this in your XAML declaration:

xmlns:system="clr-namespace:System;assembly=mscorlib"

If you want to create paths programmatically using a common path string, Silverlight is missing a bit of key functionality that WPF has -- so you have to kludge it:

string pathXaml = 
@"<Path xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
        xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
        Data=""path_data_goes_here"" />";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);

这篇关于如何在Silverlight中克隆路径资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆