VS 2019開發APP(一)界面和代碼


1.界面

在Resources>layout>目錄下的*.xml文件就是界面文件

 

 2.關聯界面

接下來,通過將支持代碼插入到 MainActivity 類中來添加代碼以關聯用戶界面。

在 MainActivity 類中找到 OnCreate 方法,在其中添加關聯按鈕代碼如下:

protected override void OnCreate(Bundle savedInstanceState)
{

/*這部分是程序自帶的代碼*/
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

//通過ID綁定控件
Button Task1 = FindViewById<Button>(Resource.Id.button1);
Button Task2 = FindViewById<Button>(Resource.Id.button2);
Button Task3 = FindViewById<Button>(Resource.Id.button3);
Button Task4 = FindViewById<Button>(Resource.Id.button4);
Button Stop= FindViewById<Button>(Resource.Id.button5);
TextView Show= FindViewById<TextView>(Resource.Id.textView1);

//添加控件對應的事件
Task1.Click += (sender, e) =>
{
Show.Text = "任務一進行中……";
};
Task2.Click += (sender, e) =>
{
Show.Text = "任務二進行中……";
};
Task3.Click += (sender, e) =>
{
Show.Text = "任務三進行中……";
};
Task4.Click += (sender, e) =>
{
Show.Text = "任務四進行中……";
};
}

現在就完成了一個簡單的響應按鈕事件的程序了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM