运行时可移动矩形窗口存储应用程序

时间:2015-08-23 15:37:58

标签: c# windows windows-runtime windows-store-apps windows-store

我正在尝试创建runt time矩形,它将在鼠标拖动时移动。由于某些原因,代码无效。

    private void Rectangle_Click(object sender, RoutedEventArgs e)
    {
        var rec = new Rectangle();
        rec.Height = 100;
        rec.Width = 100;
        rec.Fill = new SolidColorBrush(Colors.Violet);

        rec.ManipulationDelta += rec_ManipulationDelta;
        board.Children.Add(rec);

    }

    void rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        dragTranslation.X += e.Delta.Translation.X;
        dragTranslation.Y += e.Delta.Translation.Y;

        dragTranslation = new TranslateTransform();

        this.RenderTransform = this.dragTranslation;
    } 

1 个答案:

答案 0 :(得分:1)

分配变换时的问题。尝试

private void Rectangle_Click(object sender, RoutedEventArgs e)
{
    var rec = new Rectangle();
    rec.Height = 100;
    rec.Width = 100;
    rec.Fill = new SolidColorBrush(Colors.Violet);

    rec.ManipulationMode=ManipulationModes.All;
    rec.ManipulationDelta += rec_ManipulationDelta;
    rec.RenderTransform=new TranslateTransform(); // Create new TranslateTransform and assign to the rectangle
    board.Children.Add(rec);

}

void rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    Rectangle recSender = (Rectangle) sender; // Get the Rectangle
    TranslateTransform ttSender = recSender.RenderTransform as TranslateTransform; // Get the Rectangle's RenderTransform (which is a TranslateTransform)

    ttSender.X += e.Delta.Translation.X;
    ttSender.Y += e.Delta.Translation.Y;

}