我想在 WPF 中的“触摸”应用程序中画线并编写以下代码:
XAML(部分):
<Canvas x:Name="MainCanvas"
IsManipulationEnabled="True"
TouchDown="MainCanvas_TouchDown"
TouchUp="MainCanvas_TouchUp">
C#:
public partial class MainWindow : Window
{
Line myLine = new Line();
public MainWindow()
{
InitializeComponent();
}
public void MainCanvas_TouchDown(object sender, TouchEventArgs e)
{
myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
myLine.StrokeThickness = 2;
// Line's starting point
myLine.X1 = e.GetTouchPoint(this).Position.X;
myLine.Y1 = e.GetTouchPoint(this).Position.Y;
}
public void MainCanvas_TouchUp(object sender, TouchEventArgs e)
{
// Line's ending point
myLine.X2 = e.GetTouchPoint(this).Position.X;
myLine.Y2 = e.GetTouchPoint(this).Position.Y;
MainCanvas.Children.Add(myLine);
}
}
有了这个,我只能画一条线。如果我尝试绘制另一个,应用程序会崩溃。