2015-03-03 100 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;