Skip to content
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
blog.azwan082.my


Blog ini tidak lagi dikemaskini, sila ke
👉 azwan082.my 👈
untuk dapatkan kandungan terbaru.

#C#  #windows phone

Create event based component in C#

On 10 May 2014 in Tech

This post is about how to create event based component in C# for Windows Phone. Assuming that we have XAML class that interact with data model component, we’re using event to pass the data around.

In model class, create delegate and event property. Delegate is property that define the signature of callback, and event is property that other class can hook their callback to.

If you want to pass additional parameters in event handler, declare a new event argument class that extend EventArgs and assign the values when raising the event

public class DataLoadedEventArgs : EventArgs
{
	public int TotalRows { get; set; }
}

public class DataSource
{
	public delegate void DataLoadedEventHandler(object sender, DataLoadedEventArgs e);
	public delegate void DataErrorEventHandler(object sender, EventArgs e);

	public event DataLoadedEventHandler DataLoaded;
	public event DataErrorEventHandler DataError;

	public void GetDataFromServer()
	{
		// code for getting data

		// this is how to raise the event
		if (DataLoaded != null)
		{
			DataLoaded(this, new DataLoadedEventArgs()
			{
				TotalRows = 20
			});
		}

		// if has error, raise another event
		if (DataError != null)
		{
			DataError(this, new EventArgs());
		}
	}
}

Then in XAML class attach the callback to listen for the event raised from the model class, and make sure to detach the event when not used anymore.

public partial class MainPage : PhoneApplicationPage
{
	private DataSource dataSource;

	private void MainPage_Loaded(object sender, RoutedEventArgs e)
	{
		dataSource = new DataSource();
		dataSource.DataLoaded += dataSource_DataLoaded;
		dataSource.DataError += dataSource_DataError;
	}

	private void MainPage_Unloaded(object sender, RoutedEventArgs e)
	{
		dataSource.DataLoaded -= dataSource_DataLoaded;
		dataSource.DataError -= dataSource_DataError;
	}

	private void dataSource_DataLoaded(object sender, DataLoadedEventArgs e)
	{
		// use e.TotalRows
	}

	private void dataSource_DataError(object sender, EventArgs e)
	{
		// ...
	}
}
Share this post:
  • Share
  • Tweet
  • LinkedIn

Related posts:

  • Merge XAML and code-behind file in Visual Studio
  • Windows Phone app dev note
  • Using database in Windows Phone app
  • Load JSON data from web in Windows Phone
  • DataContractJsonSerializer missing assembly reference

Filed under Tech with tags C#, windows phone

Post navigation

Previous Post Previous post:
Load JSON data from web in Windows Phone
Next Post Next post:
Using database in Windows Phone app

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Media sosial

  • facebook
  • instagram
  • twitter
  • pinterest
  • youtube

Carian

Artikel popular

  • Saiz standard kayu, papan dan plywood
  • Pemasangan dinding plywood rumah kampung secara solo
  • 3 Jenis Asas Sistem Hidroponik Yang Digunakan Dalam Akuaponik Serta Variasinya
  • Kepincangan dalam menggunakan paip PVC sebagai saluran NFT hidroponik
  • Cara membuat sistem NFT untuk akuaponik – Saliran masuk
  • Cara pasang pintu rumah
  • Jenis skru kayu
  • Slack MacOS client keep crashing

Artikel terkini

  • 2 video #emau dari 2020/0221 February 2020
  • Gaya hidup minimalis14 February 2020
  • How to sync Mac OS Photos Library to an external disk storage17 January 2020
  • Sejarah dividen KWSP10 January 2020
  • Sejarah dividen Tabung Haji10 January 2020
  • Sejarah dividen ASB10 January 2020
  • Selamat dekad baru 2020-an3 January 2020
  • Dashcam Drive #17 – Taman Danu Serian → Ranchan Recreational Park1 January 2020

Tag

1N2D Akuaponik Android Apache Ayam Kampung Bash C# Cache Cili CommonJS Controller Dashcam Drive E-mau Fedora Fotografi Git Gnome Happy Together Hidroponik Idea Invincible Youth iOS Java Javascript Ke Indonesia Ke Kita? Ke Jepun Ke Kita? Ke Korea Ke Kita? Kewangan ListView Makanan Mr. Bean PHP Python Rant Roundtable Plus RPM Rumah Kampung Star Golden Bell Subversion SVN Titanium Mobile Titanium Module Titanium Studio windows phone WrestleMania

Arkib

Perihal

@azwan082

Seorang pembangun perisian (software developer) untuk peranti mudah alih (mobile devices) dan laman web, menggunakan bahasa pengaturcaraan Swift (iOS), Java (Android), PHP, MySQL dan Javascript (aplikasi web). Blog ini adalah tempat untuk saya berkongsi perkara, minat dan projek sampingan, seperti berkebun, kerja² kayu DIY, video dashcam drive dan pelbagai lagi.

© 2021 Noodlecode

Back to top
sponsored