[英]Setting Path.Data in code-behind
I have this XAML code which makes a Path
which is inside a Canvas
residing in a MainPage.xaml page.我有这个 XAML 代码,它创建了一个位于 MainPage.xaml 页面中的Canvas
内的Path
。
<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.我想要几个像这样的Path
(例如,当用户点击一个按钮时会创建一个新的Path
),所以我决定在代码隐藏中创建它们——这似乎是不可能的。
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. Path
的Data
填充了move 和 draw 命令语法,这些语法不能直接用作代码隐藏中的文本值(如上所示),就像它可以在 xaml 中一样 - 我在 Silverlight 中找到了解决方法,我在我的 Metro/Windows-Store 应用程序中尝试了相同的技术,但尽管它编译正确,但屏幕上没有Path
。
tl;dr How do I create this Path
in code-behind with the Data
being as shown ? tl; dr如何在代码隐藏中创建此Path
, Data
如图所示?
I had this probelm too a while ago in winrt. 我在winrt中有过一段时间了。 It seems that you cannot asign a "path" value directly in code behind. 似乎您不能在后面的代码中直接分配“路径”值。
However there is a solution here 但是有一个解决方案在这里
I used this class in winrt without any problem. 我在winrt中使用了此类,没有任何问题。 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. 我要做的就是更改Convert和ConvertBack方法的签名以实现IValueConverter接口,因为它在winrt中而不是在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);
Another way to do it is to use XamlReader for this with apropriate string loaded. 另一种方法是为此使用XamlReader并加载适当的字符串。 In C#6.0 it can look like this: 在C#6.0中,它可能看起来像这样:
Path pathFromCode = XamlReader.Load($"<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Path.Data>{stringPathData}</Path.Data></Path>") as Path;
I found an simpler alternative way to create a Path
in code behind.我找到了一种更简单的替代方法来在后面的代码中创建Path
。
var converter = TypeDescriptor.GetConverter(typeof(Geometry));
string pathData = "m 150,0 A 150,0 0 0 0 150,0 A 150,150 0 0 0 150,0";
var path = new Path
{
Data = (Geometry)converter.ConvertFrom(pathData),
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.