如何模拟Touch Up Inside / Outside
本文关键字:Up Inside Outside Touch 何模拟 模拟 | 更新日期: 2023-09-27 18:17:33
我目前正在将一个应用程序从iPhone移植到Windows Phone。
两个平台的按钮行为是完全不同的。
特别是在iPhone上,人们可以按下按钮并在按钮区域外释放按钮。iOS有两个事件来处理这个问题:Touch Up Inside和Touch Up Outside
然而,在Windows Phone和Silverlight上,行为是不同的,不幸的是不是我想要的。
你有Click方法在按钮被点击时被调用,当它被点击或被释放时;以某种方式(通过ClickMode值控制行为)
如果你按下按钮并释放它,而你的手指没有准确地放在按钮上,则不会发生任何事件(尽管按钮的状态会因其外观变化而改变)
最终,我想要实现的是能够在按钮被释放时触发一个事件,并且该事件应该在手指被释放时发生,就像在iPhone上一样。例如:按下按钮,将手指移到按钮外,释放手指->事件触发问题是检测何时手指离开屏幕,而不是当它只是离开按钮(这可以很容易地通过OsIsPressedChangeed检测)
如有任何意见,不胜感激
这是我如何最好地模拟类似的行为(即使手指停止在按钮顶部也会生成键释放事件)
public class CalcButton : Button
{
public delegate void CalcButtonHandler(object sender);
public event CalcButtonHandler Pressed;
public event CalcButtonHandler Released;
private bool hasFocus = false;
private bool _isPressed = false;
private bool isPressed
{
set
{
if (value != _isPressed)
{
_isPressed = value;
Debug.WriteLine(Name + (_isPressed ? " Button Is Pressed" : " Button Is Released"));
if (_isPressed)
{
// custom default press action
// here
if (Pressed != null)
{
Pressed(this);
}
}
else
{
if (Released != null)
{
Released(this);
}
}
}
}
get
{
return _isPressed;
}
}
protected override void OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine(Name + " is pressed change");
if (hasFocus && !base.IsPressed)
{
isPressed = false;
}
else if (base.IsPressed)
{
isPressed = true;
}
base.OnIsPressedChanged(e);
}
protected override void OnLostFocus(RoutedEventArgs e)
{
hasFocus = false;
_isPressed = false; // do not trigger key release event
Debug.WriteLine(Name + " lost focus");
base.OnLostFocus(e);
}
protected override void OnGotFocus(RoutedEventArgs e)
{
hasFocus = true;
Debug.WriteLine(Name + " got focus");
base.OnGotFocus(e);
}
protected override void OnHold(GestureEventArgs e)
{
_isPressed = false; // do not trigger key release event
Debug.WriteLine(Name + " on hold");
base.OnHold(e);
}
}
还有一个小问题;如果您按下一个按钮,按住它,再按下另一个按钮,则会为第一个按钮生成release事件,即使它仍处于按下状态。不知道如何绕过这个
也许你可以尝试捕获触摸输入。它似乎不能与按钮一起工作-也许它在内部做到了,但也许您可以实现自己的按钮来做到这一点。适用于矩形:
XAML:<Rectangle
x:Name="rectangle"
Fill="Yellow"
VerticalAlignment="Bottom"
HorizontalAlignment="Stretch"
Height="200"
MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"
MouseLeftButtonUp="Rectangle_MouseLeftButtonUp"/>
背后的代码:
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var success = rectangle.CaptureMouse();
}
private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
rectangle.ReleaseMouseCapture();
Point p = e.GetPosition(rectangle);
if (new Rect(0, 0, rectangle.ActualWidth, rectangle.ActualHeight)
.Contains(p))
{
Debug.WriteLine("Mouse up inside rectangle.");
}
else
{
Debug.WriteLine("Mouse up outside rectangle.");
}
}