Reputation: 35
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.
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"}
}
}
}
};
}
}
}
Upvotes: 2
Views: 13001
Reputation: 11105
If you really want inline you can do this:
new Button {
Text = "Add parameters"
Command = new Command(() => {
//Fix world hunger here
})
};
Upvotes: 7
Reputation: 1453
I never understood adding the controls inline. Perhaps you can access them later although without a name it would seem like you have to access them by some index of the stack which seems like a bad idea. So I don't have an answer exactly but rather a suggestion that would not fit well in a comment.
public MyPage: ContentPage
{
public MyPage()
{
var slayout = new StackLayout();
var button = new Button();
button.click += (o,s) => {someevent};
slayout.children.add(button);
this.Content= slayout;
}
}
Creating the controls then adding them to the layout then adding the layout as the content will allow you to access the controls.
Upvotes: 0
Reputation: 74144
Defined your button before the start of Content = new StackLayout....
:
var myButton = new Button
{
Text = "Add parameters"
};
myButton.Clicked += (object sender, EventArgs e) =>
{
System.Diagnostics.Debug.WriteLine("I've been clcked");
};
Then you add it as a child of the StackLayout
.
This code:
new StackLayout{
HorizontalOptions = LayoutOptions.Center,
Children = {
new Button{Text = "Add parameters"}
}
}
Becomes:
new StackLayout{
HorizontalOptions = LayoutOptions.Center,
Children = {
myButton
}
}
Upvotes: 0