我遵循了一个教程,并在XAML文件中定义了一个位于屏幕中央的Ellipse对象,该对象在Ellipse.RenderTransform节点中包含一个TranslateTransform节点,如下所示:
<Ellipse
x:Name="theEllipse"
Fill="White"
Width="200"
Height="200">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="theMover" />
</Ellipse.RenderTransform>
</Ellipse>在后面的代码中,我向Ellipse添加了一个ManipulationDelta事件处理程序,如下所示:
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跳跃并开始从它的初始位置而不是它的当前位置拖动。为什么会这样呢?当我第二次拖动椭圆时,无论它在哪里,我该如何定义我的拖动操作是累积的呢?
发布于 2013-01-09 18:40:36
不确定你是否已经修复了这个问题,但是这里有一个解决方案:
为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“是包含椭圆的网格布局的名称。
https://stackoverflow.com/questions/9752627
复制相似问题