2015-03-03 89 views
0

我是C#新手。我想从CS_Line类的MainPage.xaml中访问我的画布,但我无法做到。如何在类CS_Line.cs中调用该名称的画布。如何从另一个类访问MainPage的画布名称

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>

在CS_Line.cs

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

你的意思是你不能访问MyCanvas。在MainPage.xaml.cs文件中? – 2015-03-03 11:38:22

+0

锄头你添加了MyCanvas_PointerPressed事件? – Joseph 2015-03-03 11:39:11

+0

您需要从您的xaml钩住该事件 – Joseph 2015-03-03 11:41:22

回答

0

你可以从senderMyCanvas

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;