2017-01-18 7 views
0

Ich bin neu mit Xamarin und ich versuche es zu lernen, indem ich eine App, die ich mit Titanium in der Vergangenheit gebaut habe, konvertiere. Eigentlich bin ich daran gehindert, auf meinem UIView einen Swipe hinzuzufügen. Ich habe die Dokumentation überprüft und auch hier in Stack Overflow einen ähnlichen Code gefunden, der aber nicht funktioniert. Kann mir jemand zeigen, was falsch ist? ty!Xamarin - Hinzufügen von Swipe zu einem UIVIEW

public partial class ViewController : UIViewController 
{ 
    UISwipeGestureRecognizer swipe; 
    ... 
} 

private void addSwipe() 
{ 
    nfloat vpHeight = View.Bounds.Height; 
    nfloat vpWidth = View.Bounds.Width; 
    var mainContainer = new UIView() 
    { 
     Frame = new CoreGraphics.CGRect(0, 0, vpWidth, vpHeight), 
     BackgroundColor = UIColor.Red 
    }; 
    swipe = new UISwipeGestureRecognizer(); 
    mainContainer.AddGestureRecognizer(swipe); 
    if (swipe.Direction == UISwipeGestureRecognizerDirection.Up) 
    { 
     Console.WriteLine("up"); 
    } 
} 

Antwort

0

Sie benötigen einen Handler angeben, wenn Sie Ihre Erkennungs erstellen:

swipe = new UISwipeGestureRecognizer((s) => 
    { 
     if (s.Direction == UISwipeGestureRecognizerDirection.Up) 
     { 
      Console.WriteLine("up"); 
     } 
    }); 
+0

Sorry ... keine Wirkung. Ich habe meinen Code: Swipe = new UISwipeGestureRecognizer ((s) => \t \t \t { \t \t \t \t if (s.Direction == UISwipeGestureRecognizerDirection.Up) \t \t \t \t { \t \t \t \t \t Console .WriteLine ("up"); \t \t \t \t} \t \t \t}); \t \t \t mainContainer.AddGestureRecognizer (swipe); –

+0

Sie fügen die Geste an mainContainer an - aber fügen Sie diese Ansicht immer der Ansichtshierarchie hinzu? – Jason

+0

ja! this.View.Add (Hauptcontainer); –

0

ich die Lösung auf meinem eigenen gefunden ...

public partial class ViewController : UIViewController 
{ 
    UISwipeGestureRecognizer swipe; 
    ... 
} 

private void addSwipe() 
{ 
    nfloat vpHeight = View.Bounds.Height; 
    nfloat vpWidth = View.Bounds.Width; 
    var mainContainer = new UIView() 
    { 
     Frame = new CoreGraphics.CGRect(0, 0, vpWidth, vpHeight), 
     BackgroundColor = UIColor.Red 
    }; 
    UISwipeGestureRecognizer swipeUp = new UISwipeGestureRecognizer(OnSwipeUp); 
    swipeUp.Direction = UISwipeGestureRecognizerDirection.Up; 
    mainContainer.AddGestureRecognizer(swipeUp); 
} 
private void OnSwipeUp() { 
    Console.Writeline("Up"); 
} 
Verwandte Themen