2014-02-07 31 views
0

基本上,一個簡單的應用程序,當用戶觸摸屏幕時,屏幕上的計數器遞增。我沒有觸摸輸入的經驗,也沒有任何關於如何正確構建觸摸輸入的知識。至於我應該使用什麼方法或對象,我什麼也不知道。使用按鈕,觸摸會自動註冊。所以,我想與Windows Phone 8的使用Visual Studio 2013年至今在屏幕上也這樣做,這是C#代碼我迄今與它是什麼樣子的移動顯示器上的圖像:如何在C#中進行觸摸輸入?

public partial class MainPage : PhoneApplicationPage 
{ 
    int taps = 0; // create var to detect number of times, user touches the screen 

    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    // method to register the touch as the finger is placed on the screen 
    private void Canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     //Canvas c = sender as Canvas; 
    } 

    //method register the touch as the finger is lifting up from the screen 
    private void Canvas_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     //Canvas c = sender as Canvas; 
     taps++; 
     counter.Text = taps.ToString(); //convert var from int to string 
    } 

    //method register the touch as the finger leaves the area of the screen 
    private void Canvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) 
    { 
     //Canvas c = sender as Canvas; 
     MessageBox.Show("You left the screen without lifting your finger. That does not count as a tap!", "Caution!", MessageBoxButton.OK); 
    } 

    // method to reset the counter to zero when button is pressed and released 
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     taps = 0; // reset the count 
     counter.Text = taps.ToString(); // convert var from int to string 
    } 

Windows Phone 8 Display

+0

這個問題很相似,你的第二個:http://stackoverflow.com/questions/21621670/how-to-make-the-canvas-detect點觸摸輸入-適當-在-C/21624239#21624239 – Romasz

回答

0

所有的事情都是event

我建議您逐步閱讀本教程。
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207076%28v=vs.105%29.aspx

然後閱讀下面的文件。
http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.uielement.tap%28v=vs.105%29.aspx

Tap的事件將被附加到幾乎所有的UI相關的控制,例如CanvasRectangle。通過Tap事件,您可以將這些控件作爲您已經做過的一種Button

這將是所有關於你想知道的。

0

看,如果你正在尋找一個按鈕來做到這一點,則該按鈕有一個點擊事件,只要訂閱它,並增加對每一個水龍頭事件調用

喜歡參與這個 (很簡單的增量邏輯計數器這裏)

private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
     { 
      taps++; // reset the count 
      counter.Text = taps.ToString(); // convert var from int to string 
     } 

但是,如果同樣是要在屏幕上實現再放入透明畫布控制在屏幕上,並訂閱了鼠標左鍵按下事件

void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      try 
      { 
       taps++; // reset the count 
      counter.Text = taps.ToString(); // convert var from int to string 
      } 
      catch 
      { 

      } 
     } 

Canvas_MouseLeftButtonDown也將被稱爲每次點擊屏幕