所以,我是新來Xamarin形式我發現添加按鈕的方法有兩種:在Xamarin Forms中添加按鈕的最佳方式是什麼?
中的.xaml文件
<!-- <Button Text="Click Me!"
Clicked="OnButtonClicked" VerticalOptions="CenterAndExpand" />-->
1.Declaring按鈕和.xaml.cs文件
public void OnButtonClicked(object sender, EventArgs args)
{
count++;
label.Text =
String.Format("{0} click{1}!", count, count == 1 ? "" : "s");
聲明僅在.xaml.cs按鈕文件
using System; using Xamarin.Forms; namespace FormsGallery { class ButtonDemoPage : ContentPage { Label label; int clickTotal = 0; public ButtonDemoPage() { Label header = new Label { Text = "Button", Font = Font.BoldSystemFontOfSize(50), HorizontalOptions = LayoutOptions.Center }; Button button = new Button { Text = "Click Me!", Font = Font.SystemFontOfSize(NamedSize.Large), BorderWidth = 1, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.CenterAndExpand }; button.Clicked += OnButtonClicked; label = new Label { Text = "0 button clicks", Font = Font.SystemFontOfSize(NamedSize.Large), HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.CenterAndExpand }; // Accomodate iPhone status bar. this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); // Build the page. this.Content = new StackLayout { Children = { header, button, label } }; } void OnButtonClicked(object sender, EventArgs e) { clickTotal += 1; label.Text = String.Format("{0} button click{1}", clickTotal, clickTotal == 1 ? "" : "s"); } } }
但事情是:我想知道哪種方式更適合添加按鈕並且沒有任何未來的代碼問題。
謝謝!
有談論代碼VS XAML這只是事情的偏好,有的喜歡XAML則喜歡代碼的時候沒有這樣的東西更好。 – apineda
有人發佈了相同的問題xamarin論壇你有沒有看過..https://forums.xamarin.com/discussion/33175/which-is-the-best-way-to-design-the-ui-in-xamarin-形式 – GvSharma