Xamarin Forms Button OnClick

12,415

If you really want inline you can do this:

new Button {
    Text = "Add parameters"
    Command = new Command(() => {
        //Fix world hunger here
    })
};
Share:
12,415
Lala
Author by

Lala

Updated on June 04, 2022

Comments

  • Lala
    Lala almost 2 years

    I currently have a ContentPage.cs in my project, using Xamarin Forms as work environment, and I was just wondering if I could be able to add an OnClick for the button that is at the end of the code. Any help is very appreciated. Thanks in advanced.

    using System;

    using Xamarin.Forms;

    namespace DebuggerTestAndroidIOS { public class VSParameters : ContentPage { public VSParameters () { Content = new StackLayout { Children = { new StackLayout { BackgroundColor = Color.FromHex("0ADF80"), Children = {

                            new Label { Text = PatientInformation.PatientName,
                                TextColor= Color.White,
                                FontSize = 25
                            } 
                        }
                    },
    
                    //Patient Information Stack Layout
                    new StackLayout{ Orientation = StackOrientation.Horizontal, 
                        Children = {
    
                            //Patient Image, sex and date of birth
                            new StackLayout{Orientation = StackOrientation.Vertical, Padding = new Thickness (30, 0, 0, 0),
                                Children = {
    
                                    new Image {Source = "UserMale.png"},
                                    new Label {Text = "Sex: " + PatientInformation.Sex  , FontSize = 15, TextColor= Color.Black},
                                    new Label{Text = "Date of Birth: " + (PatientInformation.DateOfBirth).ToString(), FontSize = 15, TextColor= Color.Black}
                                }
    
                            },
    
                            //other patient information
                            new StackLayout{Orientation = StackOrientation.Vertical, 
                                Children = {
                                    new Label {Text = "ID: " + PatientInformation.PatientID, FontSize = 15, TextColor= Color.Black},
    
    
                                    new Label{Text = "Room: " + PatientInformation.RoomNumber, FontSize = 15, TextColor= Color.Black},
                                    new Label {Text = "Bed: " + PatientInformation.BedID, FontSize = 15, TextColor= Color.Black},
                                    new Label{Text = "Primary Doctor: " + PatientInformation.PrimaryDoctor, FontSize = 15, TextColor= Color.Black}
    
                                }
    
                            }
    
    
                        }
                    },
                    new StackLayout {Orientation = StackOrientation.Horizontal, Padding = new Thickness(30, 0, 0, 0),
                        Children = {
    
                            new StackLayout{Orientation = StackOrientation.Vertical, 
                                Children = {
                                new Label {Text ="Heart Rate", FontSize= 20, TextColor = Color.FromHex("D95D65"), HorizontalOptions = LayoutOptions.StartAndExpand},
                                    new Label{Text=""},
                                    new Label {Text ="Temperature", FontSize= 20, TextColor = Color.FromHex("08CD78"), HorizontalOptions = LayoutOptions.StartAndExpand},
                                    new Label{Text=""},
                                new Label {Text ="Respiration Rate", FontSize= 20, TextColor = Color.FromHex("08CD78"), HorizontalOptions = LayoutOptions.StartAndExpand},
                                new Label {Text ="Blood Pressure: ", FontSize= 20, TextColor = Color.FromHex("D95D65"), HorizontalOptions = LayoutOptions.StartAndExpand},
                                    new Label{Text=""},
                                    new Label{Text=""},
                                    new Label {Text ="Systolic", FontSize= 18, TextColor = Color.FromHex("D95D65")},
                                new Label {Text ="Diastolic", FontSize= 18, TextColor = Color.FromHex("D95D65")}
                                }
                            },
    
                            new StackLayout{Orientation = StackOrientation.Vertical, 
                                Children = {
                                    new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100},
                                    new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100},
                                    new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100},
                                    new Label{Text=""},
                                    new Label{Text=""},
                                    new Label{Text=""},
    
                                    new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100},
                                    new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100},
    
    
                                }
                            },
    
                        }
    
                    },
    
                    new StackLayout{HorizontalOptions = LayoutOptions.Center,
                        Children = {
    
                            new Button{Text = "Add parameters"}
    
    
                        }
    
                    }
                }
            };
    
        }
    }
    

    }