ตอนที่ 6 : การสร้าง Event Handler และ UIAlertView ของ Xamarin.iOS บน Visual Studio C# |
ตอนที่ 6 : การสร้าง Event Handler และ UIAlertView ของ Xamarin.iOS บน Visual Studio C# ในการสร้าง Event บน Xamarin.iOS ด้วย Visual Studio นั้นจะมีรูปแบบการเขียนเหมือนกับการเขียน .NET Application ประเภท Windows Form Application คือ ขั้นแรกจะต้องสร้าง Control ที่ต้องการ จากนั้นค่อยกำหนด Event ให้กับ Control นั้น ๆ ว่าจะมีเหตุการณ์หรือ Event เมื่อคลิกหรือว่ากระทำกับ Control นั้น ๆ แล้วเมื่อเกิดเหตุการณ์นั้นแล้ว เราจะใช้ Method ที่อยู่ใน Class เป็นตัว Handle เพื่อที่จะเขียนคำสั่งให้โปรแกรมทำงานใน Method นั้น ๆ แสดงคำสั่งและทำงานตามต้องการ
Xamarin.iOS UIAlertView and Event Handler
และสำหรับการโต้ตอบพื้นฐานเราจะใช้ Class ของ UIAlertView ซึ่ง Class นี้สามารถแสดง Alert Dialog แบบง่าย ๆ หรือจะ Apply ใช้กับ Confirm Dialog , Custom Dialog หรือจะเป็น Input Dialog ก็สามารถใช้ UIAlertView ได้เช่นเดียวกัน
รูปแบบการใช้งาน Xamarin.iOS กับ UIAlertView ซึ่งเป็น Class สำหรับจัดการกับ Alert Dialog
Alert Syntax
UIAlertView alert = new UIAlertView ("Title", "The message", null, "OK", null);
alert.Show();
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-01.jpg?v=1001)
Alert Confirm Syntax
var alert = new UIAlertView ("Alert Title", "Choose from two buttons", null, "OK", new string[] {"Cancel"});
alert.Clicked += (s, b) => {
label1.Text = "Button " + b.ButtonIndex.ToString () + " clicked";
Console.WriteLine ("Button " + b.ButtonIndex.ToString () + " clicked");
};
alert.Show();
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-02.jpg?v=1001)
Alert Button Custom Syntax
var alert = new UIAlertView {
Title = "custom buttons alert",
Message = "this alert has custom buttons"
};
alert.AddButton("OK");
alert.AddButton("custom button 1");
alert.AddButton("Cancel");
// last button added is the 'cancel' button (index of '2')
alert.Clicked += delegate(object a, UIButtonEventArgs b) {
Console.WriteLine ("Button " + b.ButtonIndex.ToString () + " clicked"); };
alert.Show ();
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-03.jpg?v=1001)
![](/images/adv.jpg)
กลับมายัง Xamarin.iOS บน Visual Studio
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-04.jpg?v=1001)
สร้างหน้าจอ UI ประกอบด้วย Textbox , Label และ Button ดังรูป และสร้าง ID ตามรูปภาพ
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-05.jpg?v=1001)
เราจะสร้าง Event ของ Button สามารถคลิกที่ Button เลือก Properties -> Event สามารถเลือก Event ต่าง ๆ ได้ตามต้องการ ส่วน Event ของ Button สามารถดับเบิ้ลคลิกที่ Button เพื่อสร้าง Event ได้เช่นเดียวกัน
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-06.jpg?v=1001)
Event ที่ถูกสร้างจะอยู่ในส่วนของ Class ที่เป็น .cs ที่ถูกภูกไว้กับ View
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-07.jpg?v=1001)
เมื่อกลับมาดูที่ Properties จะเห็นว่า Event ถูกสร้าง Relation ไว้กับ Method บน Class ของ .cs
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-08.jpg?v=1001)
เพิ่ม Event โต้ตอบแบบง่าย ๆ
partial void btnSubmit_TouchUpInside(UIButton sender)
{
UIAlertView alert = new UIAlertView("Hello", "Sawatdee Khun : "+ this.txtName.Text, null, "OK", null);
alert.Show();
}
ทดสอบการทำงาน
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-09.jpg?v=1001)
รันโปรแกรมบน iOS Simulator
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-10.jpg?v=1001)
ทดสอบการ Input ข้อมูล และคลิกที่ปุ่ม Submit
![Xamarin.iOS C# Event Handler UIAlertView Xamarin.iOS C# Event Handler UIAlertView](https://www.thaicreate.com/upload/tutorial/ios-c-sharp-xamarin-event-handler-alert-11.jpg?v=1001)
แสดง Event และ Alert Dialog โต้ตอบ
|