如何从一个画布中获取手势以在Windows Phone 8应用程序中以编程方式在另一个画布上设置

时间:2015-01-23 11:20:41

标签: windows-phone-8

我正在创建简单的画布应用程序,该应用程序是从画布获取签名,其名称为" ContentPanelCanvas"来自Grid的名为" pop-up"并从" Pop-up"设置该签名并在画布上设置名为" maincanvas"。 如果您知道请帮我解决此问题如何获取画布上下文并将其设置为空白画布。这是我使用的代码。

在Xaml中:

<Grid x:Name="ContentPanel">
    <Grid.RowDefinitions>
           <RowDefinition Height="*"></RowDefinition>
           <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
     </Grid.ColumnDefinitions>
     <Grid Grid.Column="0">
            <Canvas Name="maincanvas">

            </Canvas>
        </Grid>

    <Grid  Name="Popup" Visibility="Collapsed" Grid.Row="0" Width="480" Height="300" Background="Black">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
            <Canvas Name="ContentPanelCanvas"  Background="Beige">

            </Canvas>
            </Grid>
            <Grid Grid.Row="1" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" x:Name="btnOK" Content="OK" Tap="btnOK_Tap"/>
                <Button Grid.Column="1" x:Name="btnCancel" Content="Clear" Tap="btnCancel_Tap"/>
            </Grid>
        </Grid>
</Grid>

这是.CS文件:

    private Point currentPoint;
    private Point oldPoint;
    Line line;

    public SingleImage()
    {
        InitializeComponent();
        this.ContentPanelCanvas.MouseMove += new MouseEventHandler(ContentPanelCanvas_MouseMove);
        this.ContentPanelCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(ContentPanelCanvas_MouseLeftButtonDown);

    }

    void ContentPanelCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        currentPoint = e.GetPosition(ContentPanelCanvas);
        oldPoint = currentPoint;
    }

    void ContentPanelCanvas_MouseMove(object sender, MouseEventArgs e)
    {

        currentPoint = e.GetPosition(this.ContentPanelCanvas);

        line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y };

        line.Stroke = new SolidColorBrush(Colors.Purple);
        line.StrokeThickness = 3;

        this.ContentPanelCanvas.Children.Add(line);
        oldPoint = currentPoint;
    }

    private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Popup.Visibility = Visibility.Visible;
        ContentPanelCanvas.Children.Clear();
    }

    private void btnOK_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Popup.Visibility = Visibility.Collapsed;          
       // Here i want to set signature to main canvas how to pass signature
          from one canvas to another?//    
    }

    private void btnCancel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ContentPanelCanvas.Children.Clear();
    }

1 个答案:

答案 0 :(得分:1)

private void btnOK_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Popup.Visibility = Visibility.Collapsed;          
         int k = ContentPanelCanvas.Children.Count;
          for (int i = 0; i < k; i++)
          {
              line =(System.Windows.Shapes.Line)    ContentPanelCanvas.Children[0];
              line.StrokeThickness = 3;
              ContentPanelCanvas.Children.RemoveAt(0);
              maincanvas.Children.Add(line);
           }

    }