Xamarin表单按钮OnClick
我目前在我的项目中有一个ContentPage.cs,使用Xamarin Forms作为工作环境,我只是想知道我是否能够为代码末尾的按钮添加OnClick。 非常感谢任何帮助。 提前致谢。
使用系统;
使用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"} } } } }; } }
}
如果你真的想要内联,你可以这样做:
new Button { Text = "Add parameters" Command = new Command(() => { //Do something }) };
在Content = new StackLayout....
开始之前定义了你的按钮:
var myButton = new Button { Text = "Add parameters" }; myButton.Clicked += (object sender, EventArgs e) => { System.Diagnostics.Debug.WriteLine("I've been clcked"); };
然后将其添加为StackLayout
的子StackLayout
。
这段代码:
new StackLayout{ HorizontalOptions = LayoutOptions.Center, Children = { new Button{Text = "Add parameters"} } }
变为:
new StackLayout{ HorizontalOptions = LayoutOptions.Center, Children = { myButton } }
我从不明白添加内联控件。 也许您可以稍后访问它们,虽然没有名称,您似乎必须通过堆栈的某个索引访问它们,这似乎是一个坏主意。 所以我没有完全答案,而是一个不适合评论的建议。
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; } }
创建控件然后将它们添加到布局,然后添加布局作为内容将允许您访问控件。
上述就是C#学习教程:Xamarin表单按钮OnClick分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1013966.html