Quick Start Tutorial: Creating Universal Apps via Xamarin: Device Class (cont.) - Part Five

Read the previous parts of the series here,

  1. Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part One
  2. Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part Two
  3. Quick Start Tutorial: Creating Universal Apps via Xamarin: Label and Button - Part Three
  4. Quick Start Tutorial: Creating Universal Apps Via Xamarin: Device class - Part Four

This article explains part four in the series, the Device class.

Device.StartTimer

StartTimer - It helps to raise the events at a specific time period.
StartTimer

TimeSpan and callback delegates are the arguments. TimeSpan specifies the number of internal events that should raise and execute the statements and pass the callback function.

Terminate Timer- Callback function returns true. StartTimer continues the task, returns false as StartTimer stops.

Example

This example demonstrates changing the label TextColor every second.

Xaml page

Add two buttons, start and stop, and a label control.
Xaml

Code behind of the page

StartTimer will raise the event every second to call this callback function. This function is generating a random number and assigning it to the label control.
  1. private bool _isTimerStart = true;  
  2.         static readonly Random GenRandom = new Random();  
  3.         private void StartTimers()  
  4.         {  
  5.             try  
  6.             {  
  7.                 Device.StartTimer(new TimeSpan(0, 0, 1), () =>  
  8.                 {  
  9.                     LblUpdate.TextColor = Color.FromRgb(  
  10.                         GenRandom.Next(1,128),  
  11.                         GenRandom.Next(129,255),  
  12.                         GenRandom.Next(50,255));  
  13.                     return _isTimerStart;  
  14.                 });  
  15.             }  
  16.             catch {}  
  17.         }  
  18.   
  19. private void BtnStart_OnClicked(object sender, EventArgs e)  
  20.         {  
  21.             _isTimerStart = true;  
  22.             StartTimers();  
  23.         }  
  24.   
  25.         private void BtnStop_OnClicked(object sender, EventArgs e)  
  26.         {  
  27.             _isTimerStart = false;  
  28.         }  
Output

Output

Device.OpenUri

This function is used to open the URL in the default Browser.
 
function
  1. private void BtnLink_OnClicked(object sender, EventArgs e)  
  2.         {  
  3.             Device.OpenUri(new Uri("http://www.c-sharpcorner.com/"));  
  4.         }  
output

Device.BeginInvokeOnMainThread

This thread updates background thread status to the UI (Controls). UI is running in the main thread and the background task is running in the different thread. Background thread accesses the UI controls and a cross thread exception will occur.

Example

As given in the example given below, in the button click event, create the BG task (async, await). This BG task job is to update the status to the UI.

What happens in this situation is that an Application will crash.

application

To avoid this, use the BeginInvokeOnMainThread function. 

function

function


Similar Articles