gpt4 book ai didi

c# - 当我以编程方式将段添加到 Silverlight 2 中的路径时,为什么我的路径不显示?

转载 作者:行者123 更新时间:2023-11-30 12:54:10 24 4
gpt4 key购买 nike

我正在 Silverlight 中创建一个 Path,并在鼠标事件中向其添加元素。但是,尽管元素在内存中,但屏幕不会更新,直到其他原因导致屏幕重绘发生。

这是相关代码 - 我正在响应鼠标事件,并且我保留了我正在编辑的路径的类成员。

Path path = null;

private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point thisPoint = e.GetPosition(LayoutRoot);
if (path == null)
{
CreateNewPath(thisPoint);
path.LayoutUpdated += new EventHandler(path_LayoutUpdated);
}
else
{
path.AddLineElement(thisPoint);
}
}

private void CreateNewPath(Point startPoint)
{
path = new Path();
PathGeometry geometry = new PathGeometry();
path.Data = geometry;
PathFigureCollection figures = new PathFigureCollection();
geometry.Figures = figures;
PathFigure figure = new PathFigure();
figures.Add(figure);
figure.StartPoint = startPoint;
figure.Segments = new PathSegmentCollection();
path.Stroke = new SolidColorBrush(Colors.Red);
path.StrokeThickness = 2;
path.Stretch = Stretch.None;
LayoutRoot.Children.Add(path);
}

AddLineElement 是路径类的扩展方法,只是为了简化:

public static class PathHelper
{
public static void AddLineElement(this Path thePath, Point newPoint)
{
PathGeometry geometry = thePath.Data as PathGeometry;
geometry.Figures[0].Segments.Add(new LineSegment { Point = newPoint });
}
}

这是重现问题所需的最低限度。如果您在完整的 WPF 应用程序中运行此代码,它会按预期运行。单击鼠标添加立即出现的行元素。但是,在 Silverlight 中,情况就不同了。这些点击似乎什么也没做,即使单步执行代码显示数据正在添加。但是,如果您点击几次,然后调整浏览器的大小,例如,路径元素就会出现。如果你碰巧在页面上也有一个按钮,将鼠标移到上面,路径就会出现。

我已经尝试了所有明显的事情,比如在 Path(和父网格)上调用 InvalidateMeasureInvalidateArrange 到 no有用。

我唯一的解决方法是更改​​路径上的一个属性,然后再将其改回来,这似乎足以让渲染引擎绘制新的路径元素。我使用 不透明度。您必须将其设置为不同的值,否则(我认为)PropertyChanged 事件不会触发。不过,这是一种拼凑。

有没有人用这种方式玩过路径?我想如果我同时将其他图形元素放在屏幕上,这不会是一个问题,所以它可能不会影响很多人,但最好知道是否有更正确的方法来做到这一点.

最佳答案

我需要一些非常相似的东西,但使用了新的 Silverlight VE map 控件。您上面的代码工作正常,没有任何摆弄其他属性来“强制重绘”。此处代码供您引用:

using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.VirtualEarth.MapControl;

namespace MapInfo
{
public partial class Page : UserControl
{
/// <summary>
/// Sample drawing a polyline on a Virtual Earth map
/// </summary>
public Page()
{
InitializeComponent();
VEMap.MouseLeftButtonUp += new MouseButtonEventHandler(VEMap_MouseLeftButtonDown);
VEMap.MouseLeave += new MouseEventHandler(VEMap_MouseLeave);
}

MapPolyline polyline = null;

/// <summary>
/// Ends drawing the current polyline
/// </summary>
void VEMap_MouseLeave(object sender, MouseEventArgs e)
{
polyline = null;
}
/// <summary>
/// Start or add-to a polyline
/// </summary>
private void VEMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Map m = (Map)sender;

Location l = m.ViewportPointToLocation(e.GetPosition(m));

if (polyline == null)
{
CreateNewPolyline(l);
}
else
{
polyline.Locations.Add(l);
}
}
/// <summary>
/// Create a new MapPolyline
/// </summary>
/// <param name="startPoint">starting Location</param>
private void CreateNewPolyline(Location startPoint)
{
polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Red);
polyline.StrokeThickness = 2;
var lc = new LocationCollection();
lc.Add(startPoint);
polyline.Locations = lc;
VEMap.Children.Add(polyline);
}
}
}

对您眼前的情况没有帮助,但表明其他 Silverlight 的东西可以按您的预期工作。可能会帮助其他人,我希望...

关于c# - 当我以编程方式将段添加到 Silverlight 2 中的路径时,为什么我的路径不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/249866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com