C# UWP使用事件绘制不带XAML的线
我是UWP的新手。我目前正在开发一个应用程序来测试触摸屏的完整性和功能,我正在使用Grid在主页上制作的事件记录按键 Grid有一个名称Grid我正在使用事件侦听器:Grid_Pointer移动了并相应地删除了Grid_Pointer,以获取进行滑动的路径数组,以便我可以分析路径。这部分已经制作完成,目前运行良好 我所尝试的: 现在我需要在屏幕上画一条线,在屏幕上进行滑动。我试着用谷歌搜索并找到一些关于它的信息,但我找不到任何解决方案,无法根据我刚从事件侦听器中获得的一系列坐标来绘制一条直线 此外,我有单独的事件停止记录刷卡,这个事件也应该能够清除屏幕上以前的任何行。 因为每个测试都有不同数量的行,所以我也不能事先硬编码行对象。C# UWP使用事件绘制不带XAML的线,c#,uwp,line,C#,Uwp,Line,我是UWP的新手。我目前正在开发一个应用程序来测试触摸屏的完整性和功能,我正在使用Grid在主页上制作的事件记录按键 Grid有一个名称Grid我正在使用事件侦听器:Grid_Pointer移动了并相应地删除了Grid_Pointer,以获取进行滑动的路径数组,以便我可以分析路径。这部分已经制作完成,目前运行良好 我所尝试的: 现在我需要在屏幕上画一条线,在屏幕上进行滑动。我试着用谷歌搜索并找到一些关于它的信息,但我找不到任何解决方案,无法根据我刚从事件侦听器中获得的一系列坐标来绘制一条直线 此
欢迎提供任何有关如何在不使用XAML的情况下简单地绘制一条线并依赖于已生成的事件的适当帮助。如果有一组点,可以从这些点绘制一条线并将其添加到网格中
private void CreatePolyline()
{
// Initialize a new Polyline instance
Polyline polyline = new Polyline();
// Set polyline color
polyline.Stroke = new SolidColorBrush(Colors.Black);
// Set polyline width/thickness
polyline.StrokeThickness = 10;
// Initialize a point collection
var points = new PointCollection();
points.Add(new Point(20, 100));
points.Add(new Point(35, 150));
points.Add(new Point(60, 200));
points.Add(new Point(90, 250));
points.Add(new Point(40, 300));
// Set polyline points
polyline.Points = points;
// Finally, add the polyline to layout
grid.Children.Add(polyline);
}
或者,为了测试触摸屏的完整性,您可以使用另一个用户控件,而无需以编程方式绘制线条
首先,删除所有已经完成的代码,这样就有了一个空白的网格“Grid”
在设计器中,将InkCanvas
控件拖动到“网格”中
并从代码隐藏中启用触摸功能:
inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Touch;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
rootGrid.PointerPressed += RootGrid_PointerPressed;
rootGrid.PointerMoved += RootGrid_PointerMoved;
rootGrid.PointerReleased += RootGrid_PointerReleased;
rootGrid.PointerExited += RootGrid_PointerExited;
}
int pointerId = -1;
Line curLine = null;
private void RootGrid_PointerExited(object sender, PointerRoutedEventArgs e)
{
OnComplete();
}
private void RootGrid_PointerReleased(object sender, PointerRoutedEventArgs e)
{
OnComplete();
}
private void OnComplete()
{
// reset pointerId so that other pointers may enter live rendering mode
pointerId = -1;
curLine = null;
}
private void RootGrid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
var pointerPoint = e.GetCurrentPoint(rootGrid);
if (pointerId == (int)pointerPoint.PointerId)
{
curLine.X2 = pointerPoint.Position.X;
curLine.Y2 = pointerPoint.Position.Y;
curLine.Stroke = new SolidColorBrush(Colors.Red);
}
}
private void RootGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Make sure no pointer is already drawing (we allow only one 'active' pointer at a time)
if (pointerId == -1)
{
var pointerPoint = e.GetCurrentPoint(rootGrid);
if (!pointerPoint.Properties.IsLeftButtonPressed)
return;
curLine = new Line();
var position = pointerPoint.Position;
curLine.X1 = pointerPoint.Position.X;
curLine.Y1 = pointerPoint.Position.Y;
curLine.StrokeThickness = 1;
rootGrid.Children.Add(curLine);
//save pointer id so that no other pointer can ink until this one is released
pointerId = (int)pointerPoint.PointerId;
}
}
}
仅此而已
要清除InkCanvas
中的墨水,您只需在您提到的单独事件中调用它
inkCanvas.InkPresenter.StrokeContainer.Clear();
这里有一个演示给你,希望它能帮助你 XAML页面:
<Grid x:Name="rootGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
</Grid>
你已经有了一个XAML网格,那么你为什么不能使用XAML行呢?我想OP的意思是他想在代码隐藏中完成它。非常感谢你基于这段代码,我让它工作起来了,而且画多段线也很好。可悲的是,实现这个代码游戏让我又头疼了一件事:在向网格中添加子元素时,这会在某种程度上阻碍我的应用程序的一些其他功能。我的应用程序还有TCP-server功能,基于MSDN中的示例代码:现在,如果我将子项添加到网格中,它将阻止与TCP服务器的通信。如何解除阻止?您需要在UI线程中向网格添加行,并在另一个线程中添加行以与TCP服务器通信。