如何累计拖动Ellipse对象?

时间:2012-03-17 18:28:24

标签: windows-phone-7.1

我正在关注一个教程并定义了(在XAML文件中)位于屏幕中心的Ellipse对象,其中包含TranslateTransform节点内的Ellipse.RenderTransform节点如下:

<Ellipse
  x:Name="theEllipse"
  Fill="White"
  Width="200"
  Height="200">
  <Ellipse.RenderTransform>
    <TranslateTransform x:Name="theMover" />
  </Ellipse.RenderTransform>
</Ellipse>

在后面的代码中,我向ManipulationDelta添加了Ellipse事件处理程序,如下所示:

public MainPage()
{
// other stuff
theEllipse.ManipulationDelta
  += new EventHandler<ManipulationDeltaEventArgs>(theEllipse_ManipulationDelta);
}

void theEllipse_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
theMover.X = e.CumulativeManipulation.Translation.X;
theMover.Y = e.CumulativeManipulation.Translation.Y;
}

因此,我可以按下Ellipse并将其从开始位置拖动。然而,我发现,当我释放Ellipse并再次按下它时,Ellipse跳跃并开始从其初始位置而不是当前位置拖动。为什么是这样?那么我如何定义我的拖动动作是累积的,因为当我第二次拖动椭圆时,它包含在哪里?

1 个答案:

答案 0 :(得分:0)

不确定您是否已修复此问题,但这是一个解决方案:

为manipulationStarting添加事件处理程序,并将manipulationContainer设置为其母亲。

<Window x:Class="TempProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="768" Width="640"
    ManipulationStarting="window_ManipulationStarting"
      ManipulationDelta="window_ManipulationDelta"
     >
<Grid x:Name="canvas">
    <Ellipse
x:Name="theEllipse"
Fill="Black"
Width="200"
Height="200"
IsManipulationEnabled="True">
<Ellipse.RenderTransform>
            <TranslateTransform x:Name="theMover" />
        </Ellipse.RenderTransform>
    </Ellipse>
</Grid>
</Window>

该功能应该是这样的:

private void window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        theMover.X = e.CumulativeManipulation.Translation.X;
        theMover.Y = e.CumulativeManipulation.Translation.Y;
        e.Handled = true;
    }

    private void window_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
    {
        e.ManipulationContainer = canvas;
        e.Handled = true;
    }

其中“canvas”是包含椭圆的网格布局的名称。

相关问题