gpt4 book ai didi

c# - 在 Xamarin Forms 中添加按钮的最佳方式是什么?

转载 作者:行者123 更新时间:2023-12-05 09:18:47 26 4
gpt4 key购买 nike

所以我是 Xamarin Forms 的新手,我发现了两种添加按钮的方法:

1.在.xaml文件中声明按钮

 <!--  <Button Text="Click Me!"
Clicked="OnButtonClicked" VerticalOptions="CenterAndExpand" />-->

和 .xaml.cs 文件

 public void OnButtonClicked(object sender, EventArgs args)
{
count++;

label.Text =
String.Format("{0} click{1}!", count, count == 1 ? "" : "s");
  1. 仅在 .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");
    }
    }
    }

但问题是:我想知道哪种方式更适合添加按钮并且不会出现任何 future 的代码问题。

谢谢!

最佳答案

其实它们是一样的。这取决于一个人的选择。

我更喜欢 XAML 而不是代码,因为

  • XAML 更清晰且易于理解。
  • XAML 似乎能够更好地尊重 UI 和 Controller 逻辑之间的“关注点分离”。
  • 它在 Visual Studio 中具有智能感知

您可以在这里找到您的详细答案 https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/summaries/chapter07/

关于c# - 在 Xamarin Forms 中添加按钮的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com