我是C#新手。我想从CS_Line类访问MainPage.xaml中的Canvas,但我不能这样做。如何在类CS_Line.cs中调用名称的画布。
MainPage.xaml中:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="573" Margin="154,76,0,0" VerticalAlignment="Top" Width="913" Background="White"/>
</Grid>
:
class CS_Line
{
//attribute
InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
private uint _penID;
private uint _touchID;
private Point _previousContactPt;
private Point currentContacPt;
private double x1;
private double y1;
private double x2;
private double y2;
//method
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas); *//====>>>>> can't access the MyCanvas*
_previousContactPt = pt.Position;
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
_inkKhaled.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
}
}
答案 0 :(得分:0)
您可以从MyCanvas
获取sender
。
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
Canvas myCanvas = sender as Canvas; // Here's your MyCanvas object
PointerPoint pt = e.GetCurrentPoint(myCanvas);
_previousContactPt = pt.Position;