[英]Moving an object according to the direction button in WP8 App
I'm trying to move an object according to the direction buttons Up,Left,Right,Down. 我正在尝试根据方向按钮上,左,右,下移动对象。
I am setting the margin property like:- 我将保证金属性设置为:-
img.Margin = new Thickness(l, t, r, b); //L T R B
I am incrementing/decrementing the values according to the desired movement needed. 我正在根据所需的所需移动来增加/减少这些值。
I am able to move the object through the click event. 我可以通过click事件移动对象。 However, I'd like to move the object in the desired direction whenever the button is pressed and held for the user. 但是,无论何时为用户按下并按住按钮,我都想沿所需方向移动对象。 As soon as the user releases the button the movement should also stop. 一旦用户释放按钮,运动也应停止。
I tried using the hold event, but the operation executed once and then stopped. 我尝试使用hold事件,但是该操作执行一次然后停止。
On another attempt I tried looping my statements but the App stalled. 在另一次尝试中,我尝试循环我的语句,但该应用程序停顿了。
Kindly help me out. 请帮我。 Thanks! 谢谢!
EDIT:- 编辑:-
I handled the ManipulationStarted,ManipulationDelta,ManipulationCompleted events. 我处理了ManipulationStarted,ManipulationDelta,ManipulationCompleted事件。
Now, I'm able to move my object whenever I'm pressing and holding the button. 现在,只要按住按钮,就可以移动对象。 However, the new problem that I'm facing is that I have to constantly keep my finger moving on the screen so as to perform the motion. 但是,我面临的新问题是必须不断保持手指在屏幕上移动才能执行动作。
The code for the Up Button(the button that moves the object in the vertical direction) is:- 向上按钮(沿垂直方向移动对象的按钮)的代码为:-
public double l = 0.0, t = 0.0, r = 0.0, b = 0.0;
public void move()
{
img.Margin = new Thickness(l, t, r, b); //L T R B
}
private void up_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
}
private void up_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
t = t + 1.0;
move();
}
private void up_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
}
I'm not sure whether this method is correct or not. 我不确定此方法是否正确。 Do advise. 劝告。 Thanks. 谢谢。
You should use ManipulationStarted
and ManipulationCompleted
events.您应该使用 ManipulationStarted
和ManipulationCompleted
事件。They works for Tap
and Hold
gestures as stated here: https://msdn.microsoft.com/en-us/library/windows/apps/ff426933(v=vs.105).aspx它们适用于此处所述的“ Tap
并Hold
手势: https : //msdn.microsoft.com/zh-cn/library/windows/apps/ff426933(v=vs.105).aspx
Update 更新
To correctly detect the beginning and the end of a tap, I suggest using MouseEnter
and MouseLeaving
events. 为了正确检测敲击的开始和结束,我建议使用MouseEnter
和MouseLeaving
事件。 Here a sample which shows how I'm moving down an object. 这里有一个示例,显示了我如何向下移动一个对象。 This is currently a square in the centre of the screen: 当前这是屏幕中央的正方形:
<Grid x:Name="objectToMove" Background="red" Height="100" Width="100">
<Grid.RenderTransform>
<TranslateTransform x:Name="verticalTransform" Y="0" />
</Grid.RenderTransform>
</Grid>
The code behind: 后面的代码:
AutoResetEvent autoEvent = new AutoResetEvent(true);
System.Threading.Timer dt;
private void toggle_MouseEnter(object sender, MouseEventArgs e)
{
dt = new System.Threading.Timer(new TimerCallback(MoveFunct), autoEvent, 0, 1);
}
private void MoveFunct(Object stateInfo)
{
Deployment.Current.Dispatcher.BeginInvoke(() => { verticalTransform.Y += 3; });
}
private void toggle_MouseLeave(object sender, MouseEventArgs e)
{
dt.Dispose();
}
Note that the last parameter of Timer
constructor consists in the interval between ticks. 请注意, Timer
构造函数的最后一个参数包括刻度之间的间隔。 Also inside MoveFunct
function, I'm calling a Dispatcher
method otherwise it could not access UI thread. 同样在MoveFunct
函数内部,我正在调用Dispatcher
方法,否则它将无法访问UI线程。 In the sample I used a TranslateTransform
which is better, in my opinion, than Margin
manipulation because it requires to update the element whole visual tree. 我认为在示例中,我使用了TranslateTransform
,它优于Margin
操作,因为它需要更新整个元素的可视树。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.