Bengi Besceli
Bengi Besceli

Reputation: 3748

Handle button click event in Xamarin

I'm new in Xamarin and not so good at English for reading the related mannuels.
Could someone express me how do I handle this button's click event method in code behind for Xamarin?

public class App : Application
{
    public App ()
    {
        // The root page of your application
        MainPage =new ContentPage{
            Content= new Button{Text="Click me",BackgroundColor=Color.Black,HorizontalOptions=LayoutOptions.Center,VerticalOptions=LayoutOptions.Center,TextColor=Color.White}
    };
}

Upvotes: 1

Views: 14027

Answers (1)

Sreeraj
Sreeraj

Reputation: 2434

public class App : Application
{
    public App ()
    {
        // The root page of your application
        MainPage =new LaunchPage();
    }
}
public class LaunchPage:ContentPage
{
    public LaunchPage ()
    {
        var button=new Button{Text="Hello World",BackgroundColor=Color.Black,HorizontalOptions=LayoutOptions.Center,VerticalOptions=LayoutOptions.Center,TextColor=Color.White};
        button.Clicked += ButtonClicked;
        Content = button;
    }
    void ButtonClicked(object sender, EventArgs args)
    {
        DisplayAlert ("Button Clicked", "This Button has been clicked", "OK");
    }
}

Upvotes: 4

Related Questions