它返回一个几何,但那是一个抽象类。我正在创建一个文本形状并想要缓存这个几何图形。
这BuildGeometry
可能会返回不同的类型,感觉很可疑,因此我可能做错了什么。我可以破坏代码(一旦运行就会破坏),但我怎么能确定它总是属于同一类型?
public class Label : ShapeBase
{
RectangleGeometry geometry = new RectangleGeometry();
protected override Geometry DefiningGeometry
{
get { return geometry; }
}
protected override Size MeasureOverride(Size constraint)
{
return constraint;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(Label), new UIPropertyMetadata(string.Empty, OnTextChanged));
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Label label = (Label)d;
label.SetGeometry(label.Text);
label.InvalidateVisual();
}
private void SetGeometry(string text)
{
FormattedText formattedtext = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Tahoma"), 16, Brushes.Black);
GeometryGroup group = (GeometryGroup)formattedtext.BuildGeometry(new Point(0, 0));
}
}