2011-12-13 59 views
0

我目前正在將應用程序從iPhone移植到Windows Phone。如何模擬Touch Up內部/外部

按鈕的行爲在兩個平臺之間是完全不同的。

特別是,在iPhone上,可以按下按鈕並釋放按鈕區域外的按鈕。 iOS有兩個事件來處理這個問題:Touch Up Inside和Touch Up Outside

但是,在Windows Phone和Silverlight上,行爲是不同的,不幸的是不是我想要的。

您已經點擊按鈕時調用的Click方法,可以是實際點擊或釋放按鈕; (通過ClickMode值控制的行爲)

如果您按下按鈕並釋放它,而手指不在按鈕上,則不會發生任何事件(儘管按鈕的狀態會按照已證明的方式發生變化通過它的外觀變化)

最終,我想實現的是能夠觸發一個事件,當按鈕被釋放時,這個事件應該發生在任何情況下手指被釋放,就像它在iPhone上一樣。 例如: 按下按鈕,移動手指的按鈕外,鬆開手指 - >事件觸發 當手指離開屏幕,而不是當它只是離開按鈕(其可很容易地通過被檢測到的問題是檢測OsIsPressedChangeed)

任何想法whoulc不勝感激

回答

0

下面是我最好的管理,以模擬出類似的行爲(正在生成鍵釋放事件即使在手指不再是按鈕的頂部)

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); 
    } 
} 

還有一個小問題;如果按下按鈕,按住按鈕,按下另一個按鈕,即使仍然按下第一個按鈕,也會生成Released事件。不知道如何解決這個問題

0

也許你可以嘗試捕捉觸摸輸入。它似乎沒有與一個按鈕的工作 - 也許它內部做它,但也許你可以實現自己的按鈕。 作品帶有長方形:

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."); 
    } 
} 
+0

謝謝,但這是Silverlight的Windows Phone上;沒有鼠標事件.. – jyavenard

+0

有鼠標事件。這是我猜測桌面Silverlight的一次不幸的過渡,但是如果您想要處理單點觸摸操作,則可以使用鼠標事件。我認爲它不適用於多點觸控 - 在這種情況下,您需要使用Touch.FrameReported + =(s,e)=> {};您還可以查看操縱事件(ManipulationStarted/Completed)。 –