如何从另一个类访问MainPage的名称画布

时间:2015-03-03 11:30:45

标签: c# xaml canvas windows-phone-8.1

我是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>

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)
        {

        }
    }

1 个答案:

答案 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;