簡體   English   中英

如何在 Xamarin.Forms 中添加按鈕?

[英]How to add a button in Xamarin.Forms?

我是 Xamarin 的新手。
你能寫我在 Xamarin.Forms 中添加按鈕的代碼嗎?

也許你應該從這個介紹開始。 它解釋了如何開始使用 Xamarin 表單。

https://developer.xamarin.com/guides/xamarin-forms/getting-started/introduction-to-xamarin-forms/

還有Xamarin 表單快速入門,其中包含一個向 UI 添加按鈕的示例。

從示例:

xml:

<Button x:Name="translateButon" Text="Translate" Clicked="OnTranslate" />

cs中的綁定命令:

void OnTranslate (object sender, EventArgs e)
    {
        translatedNumber = Core.PhonewordTranslator.ToNumber (phoneNumberText.Text);
        if (!string.IsNullOrWhiteSpace (translatedNumber)) {
            callButton.IsEnabled = true;
            callButton.Text = "Call " + translatedNumber;
        } else {
            callButton.IsEnabled = false;
            callButton.Text = "Call";
        }
    }
public class App : Application
{
    public App ()
    {
        // The root page of your application
        MainPage =new ContentPage{
            Content= new Button{Text="Hello World",BackgroundColor=Color.Black,HorizontalOptions=LayoutOptions.Center,VerticalOptions=LayoutOptions.Center,TextColor=Color.White}
        };
    }


}

上面的代碼片段將創建一個帶有 Button 的頁面,該頁面的文本Hello World在頁面中水平和垂直居中,並將其設置為應用程序的 LaunchPage。 這是如何以編程方式擁有按鈕的示例。

按鈕就像

Button b = new Button();

然后你應該在布局中添加一個按鈕

StackLayout SL = new StackLayout();
SL.Children.Add(b);

然后將您的內容頁面設置為 stacklayout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM