在设置Path.Data代码隐藏 [英] Setting Path.Data in code-behind
问题描述
我有这样的XAML代码,这使得一个路径
这是在画布
居住在MainPage.xaml中页
I have this XAML code which makes a Path
which is inside a Canvas
residing in a MainPage.xaml page.
<Path x:Name="progressPath" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stroke="Gold" StrokeThickness="5"
Canvas.Left="300" Canvas.Top="300" Height="305" Width="305"
Data="m 150,0 A 150,0 0 0 0 150,0 A 150,150 0 0 0 150,0">
</Path>
我想有几个路径
的像这样的(例如,一个新的路径
是当用户点击一个按钮制造),所以我决定在代码隐藏创建它们 - 这似乎并不就可以了。
I want to have several Path
's like this one (for example, a new Path
is made when the user taps a button), so I decided to create them in the code-behind - which doesn't seem to be possible.
路径的
填充了移动和借鉴,不能直接被用作语法命令文本值(如上图所示)代码隐藏像它可以在XAML - 我发现的这个在Silverlight和我的解决方法试过同样的技术在我的地铁/ Windows的应用程序商店,但尽管它编译正确,没有数据
路径$ 。C $ C>在屏幕上
The Path
's Data
is populated with the move and draw commands syntax which cannot directly be used as a text value (as shown above) in code-behind like it can be in xaml - I've found workarounds for this in Silverlight and I tried the same technique in my Metro/Windows-Store app but though it compiles correctly there is no Path
on the screen.
TL;博士如何创建这个中的代码隐藏与路径
的数据
是如图所示?
tl;dr How do I create this Path
in code-behind with the Data
being as shown ?
推荐答案
我在WinRT中有这个万阿英,蒋达清太前一阵子。看来,你不能直接在代码ASIGN了路径值后面。
I had this probelm too a while ago in winrt. It seems that you cannot asign a "path" value directly in code behind.
然而,有一个解决方案的这里
However there is a solution here
我用这个类WinRT中没有任何问题。所有我必须做的是改变转换的签名和ConvertBack方法来实现的IValueConverter接口,因为它是在WinRT中,而不是在Silverlight。
在这里,他们是
I used this class in winrt without any problem. All I had to do is change the signatures of the Convert and ConvertBack methods to implement the IValueConverter interface as it is in winrt and not in silverlight. Here they are
public object Convert(object value, Type targetType, object parameter, string language)
{
string path = value as string;
if (null != path)
return Convert(path);
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
PathGeometry geometry = value as PathGeometry;
if (null != geometry)
return ConvertBack(geometry);
else
return default(string);
}
用法:(或多或少)
Usage: (More or less)
var stringToPathGeometryConverter = new StringToPathGeometryConverter();
string pathData = "m 150,0 A 150,0 0 0 0 150,0 A 150,150 0 0 0 150,0" ;
progressPath.Data = stringToPathGeometryConverter.Convert(pathData);
这篇关于在设置Path.Data代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!