2013-05-29 122 views
5

在Windows Phone 8中檢測觸摸利用了System.Windows.Input.Touch.FrameReported事件,它是開發人員可用的最原始且最響應的觸摸事件。因爲只有一個單點觸摸在同一時間長 -如何檢測兩個同時觸摸?

public MainPage() 
{ 
    InitializeComponent(); 

    // setup sounds 
    Ellipse1.Tag = new Uri("Sounds/GVD_snr1.wav", UriKind.Relative); 
    Ellipse2.Tag = new Uri("Sounds/GVD_snr2.wav", UriKind.Relative); 
    Ellipse3.Tag = new Uri("Sounds/GVD_snr3.wav", UriKind.Relative); 
    Ellipse4.Tag = new Uri("Sounds/GVD_snr4.wav", UriKind.Relative); 
    Ellipse5.Tag = new Uri("Sounds/GVD_snr5.wav", UriKind.Relative); 
    Ellipse6.Tag = new Uri("Sounds/GVD_snr6.wav", UriKind.Relative); 
    Ellipse7.Tag = new Uri("Sounds/Gong.wav", UriKind.Relative); 

    // respond to touch(es) 
    var _Ellipses = new[] { Ellipse1, Ellipse2, Ellipse3, Ellipse4, Ellipse5, Ellipse6, Ellipse7 }; 
    System.Windows.Input.Touch.FrameReported += (s, e) => 
    { 
     var _Touches = 
      from touch in e.GetTouchPoints(null) 
      where touch.Action == System.Windows.Input.TouchAction.Down 
      let ellipse = touch.TouchDevice.DirectlyOver as Ellipse 
      where _Ellipses.Contains(ellipse) 
      select ellipse; 
     System.Diagnostics.Debug.WriteLine("{0} touch(es).", _Touches.Count()); 
     foreach (var ellipse in _Touches) 
     { 
      var _Stream = Application.GetResourceStream(ellipse.Tag as Uri).Stream; 
      var _SoundEffect = Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(_Stream); 
      Microsoft.Xna.Framework.FrameworkDispatcher.Update(); 
      _SoundEffect.Play(); 
     } 
    }; 
} 

(用的Lumia 920測試)

這就像一個魅力:

你會使用這樣的事件。當用戶試圖同時觸摸兩個或更多點時(我的意思是完全相同的時間),事件根本不會發生。當用戶幾乎同時嘗試觸摸兩個或更多點時(僅相隔一秒),則會引發事件並報告兩個點。

如何檢測兩個同時觸摸?

在你想看到的XAML情況下,這裏的XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 

    <Grid.Resources> 
     <Style TargetType="Ellipse"> 
      <Setter Property="HorizontalAlignment" Value="Left" /> 
      <Setter Property="VerticalAlignment" Value="Top" /> 
     </Style> 
    </Grid.Resources> 

    <Ellipse x:Name="Ellipse1" Fill="Blue" Height="177" Margin="17,17,0,0" Width="177"/> 
    <Ellipse x:Name="Ellipse2" Fill="#FFFFA300" Height="223" Margin="212,25,0,0" Width="223"/> 
    <Ellipse x:Name="Ellipse3" Fill="#FFFF00E8" Height="97" Margin="89,207,0,0" Width="97"/> 
    <Ellipse x:Name="Ellipse4" Fill="#FF00C135" Height="162" Margin="186,249,0,0" Width="162"/> 
    <Ellipse x:Name="Ellipse5" Fill="#FF00AEFF" Height="272" Margin="59,416,0,-81" Width="272"/> 
    <Ellipse x:Name="Ellipse6" Fill="Red" Height="97" Margin="320,395,0,0" Width="97"/> 
    <Ellipse x:Name="Ellipse7" Fill="#FFF3FF00" Height="133" Margin="10,304,0,0" Width="133"/> 

</Grid> 
+0

@Jerry ..你試試這個?在實例變量中有lastTappedTime標記。處理所有橢圓中的Tap事件,並檢查lastTappedTime和現在時間之間的差異。如果差值小於200毫秒,我們可以放心地認爲它是Simul-Touch,否則設置新的lastTappedTime。 – kanchirk

+0

更清楚的是,問題並不在於它們是否同時發生,而是兩個同時觸摸不會引發FrameReported事件。 –

+0

謝謝澄清。 – kanchirk

回答

0

添加此解決了這個問題:

var _Timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(50) }; 
_Timer.Tick += (s, e) => 
{ 
    try { Microsoft.Xna.Framework.FrameworkDispatcher.Update(); } 
    catch { } 
}; 
_Timer.Start();