2 回答
TA贡献1829条经验 获得超9个赞
这是给你的demo,希望对你有帮助。
XAML 页面:
<Grid x:Name="rootGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
</Grid>
后面的代码:
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;
}
}
}
TA贡献1785条经验 获得超4个赞
您有一个点集合,您可以从这些点绘制一条折线并将其添加到网格中。
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);
}
或者,为了测试触摸屏的完整性,您可以使用另一个用户控件InkCanvas,而无需以编程方式绘制线条。
首先,删除你所做的所有代码,这样你就有了一个空白的 Grid 'grid'。
在设计器中,将InkCanvas控件拖动到“网格”。
并从后面的代码启用触摸功能:
inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Touch;
就这些。
要清除 中的墨水,您可以在您提到的单独事件InkCanvas中简单地调用它,
inkCanvas.InkPresenter.StrokeContainer.Clear();
- 2 回答
- 0 关注
- 151 浏览
添加回答
举报