Windows Phone 8.1 RT:如何防止操纵事件抛出对象?

Windows Phone 8.1 RT: how to prevent manipulation event throw object?

我是 C# 初学者。我想防止通过操作事件抛出 XAML 的对象。我的应用程序是在 Windows Phone 8.1 RT 中开发的。

我有 XAML 的矩形:

<Canvas x:Name="MyCanvas" Background="White">
        <Rectangle Name="TestRectangle"
          Width="100" Height="200" Fill="Blue" 
          ManipulationMode="All"/>
</Canvas>

在主页中:

public MainPage()
    {
        this.InitializeComponent();
        // Handle manipulation events.
        TestRectangle.ManipulationDelta += Drag_ManipulationDelta;
        dragTranslation = new TranslateTransform();
        TestRectangle.RenderTransform = this.dragTranslation;

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
void Drag_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        // Move the rectangle.
            dragTranslation.X += e.Delta.Translation.X;
            dragTranslation.Y += e.Delta.Translation.Y;
    }

    private void TestRectangle_PointerPressed(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Change the size of the Rectangle
        if (null != rect)
        {
            rect.Width = 250;
            rect.Height = 150;
        }
    }
    private void TestRectangle_PointerReleased(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Reset the dimensions on the Rectangle
        if (null != rect)
        {
            rect.Width = 200;
            rect.Height = 100;
        }
    }
    private void TestRectangle_PointerExited(object sender,
PointerRoutedEventArgs e)
    {
        Rectangle rect = sender as Rectangle;

        // Finger moved out of Rectangle before the pointer exited event
        // Reset the dimensions on the Rectangle
        if (null != rect)
        {
            rect.Width = 200;
            rect.Height = 100;
        }
    }

如何在用户退出事件时移动对象而不抛出?

我假设 'throwing' 你的意思是你想移除矩形的惯性方面?在这种情况下,从 XAML 代码中删除 ManipulationMode setter 并将此行插入页面的构造函数 (C#):

TestRectangle.ManipulationMode = ManipulationMode.TranslateX | ManipulationMode.TranslateY;

这应该可以消除您所说的惯性效应。附带说明一下,这种移动 UIElement 的方法会给您带来非常烦人的延迟。不久前我遇到了类似的问题,并通过使用不同的方法解决了这个问题: