有兩種可能性(據我所知)事件:
您可以使用XNA的TouchPanel(有關它的更多信息,你可以在這裏找到:Working with TouchInput,Detecting Gestures和這blog)本:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick;
myWebbrowser.ManipulationCompleted += myWebbrowser_ManipulationCompleted;
}
private void myWebbrowser_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.Flick:
if (e.FinalVelocities.LinearVelocity.X < 0)
LoadNextPage();
if (e.FinalVelocities.LinearVelocity.X > 0)
LoadPreviousPage();
break;
default:
break;
}
}
}
或者你可以使用Silverlight Toolkit作爲this answer或other here描述。
編輯 - 小此言
只有看出來,因爲當你使用XNA,你有時需要(例如的OnNavigatedTo)做:
FrameworkDispatcher.Update();
否則你的應用程序有時會崩潰。
希望這會有所幫助。
EDIT 2 - 註釋後的代碼示例
如果您ManipulationEvent首先handeled(例如,通過透視或地圖),我想訂閱Touch.FrameReported
- 因爲我已經測試 - 它應該工作:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick;
Touch.FrameReported += Touch_FrameReported;
}
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.Flick:
if (gesture.Delta.X > 0)
MessageBox.Show("Right");
if (gesture.Delta.X < 0)
MessageBox.Show("Left");
break;
default:
break;
}
}
}
下一個編輯(後下一個評論) - 更好地實施和禁用例如向上,向下的手勢:
如果你不想激活了(小左)/下(LITT對),你必須自己描述條件。請注意,有一個很小的機會(如果有的話),用戶只會左右移動他的手指/上/下 - 所以你必須包括一些保證金。有很多解決方案,你必須嘗試和它一起玩,最簡單的解決方案只是比較手勢的deltaX和deltaY:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick | GestureType.HorizontalDrag;
Touch.FrameReported += Touch_FrameReported;
}
TouchPoint firstPoint;
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint mainTouch = e.GetPrimaryTouchPoint(ContentPanel);
if (mainTouch.Action == TouchAction.Down) firstPoint = mainTouch;
else if (mainTouch.Action == TouchAction.Up && TouchPanel.IsGestureAvailable)
{
double deltaX = mainTouch.Position.X - firstPoint.Position.X;
double deltaY = mainTouch.Position.Y - firstPoint.Position.Y;
if (Math.Abs(deltaX) > 2 * Math.Abs(deltaY))
{
if (deltaX < 0) MessageBox.Show("Right.");
if (deltaX > 0) MessageBox.Show("Left.");
}
}
}
如何在模擬器上產生這種效果? –
@JMat正如我測試過的 - 在模擬器上,當你單擊(不保持)在屏幕上(或你的瀏覽器,我訂閱了MainPage_Manipulation,因此可以在整個屏幕上工作),並從左側快速移動鼠標或rigt。試一試。 – Romasz
我測試過了,但沒有工作。可能是我正在收聽DragCompleted等其他一些事件(但無法找到它的原因)。你可以發佈示例項目/完整頁面代碼的地方,所以我可以運行和測試 –