How to Add Click event in Stack Layout or Frame

前端 未结 2 870
天涯浪人
天涯浪人 2021-02-03 20:03

I am new in xamarin.forms please help me out how i can add click event in Stack Layout or Frame



        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-03 20:35

    You can add a TapGestureRecognizer to the StackLayout in XAML like this:

    
        
            
        
    
    

    Then you can implement the OnTapped method in the code behind:

    void OnTapped(object sender, EventArgs e) 
    {
        // Do stuff
    }
    

    Alternatively, if you are using the MVVM pattern and would like to Bind the tap to an ICommand in the ViewModel, that can be achieved like this:

    
        
            
        
    
    

    In your ViewModel you would have:

    private ICommand _tapCommand;
    pubic ICommand TapCommand => (_tapCommand ?? _tapCommand = new Command(OnTapped));
    
    void OnTapped() 
    {
        // Do stuff
    }
    

    There are some really good guides on the Xamarin website:

    http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/gestures/#Using_Xaml

    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/

提交回复
热议问题