Switch Cell In Xamarin.Forms Application For Android And UWP (by Vijayaragavan S)

Switch Cell contains a label and an on/off switch. Reading this article, you can learn how to use Switch Cells with Table View in Xamarin.Forms application for Android and Universal Windows Platform in cross platform application development.

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended)
  2. Visual Studio 2017 RC Community/Enterprise/Professional Edition (It is a Free trial software available online)
  3. Using Visual Studio 2017 Installer, enable the feature of mobile development with .NET.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2017 RC and go to Start -> New Project-> select Cross-Platform (under Visual C#->Cross Platform App-> Give suitable name for your app (as – XamFormSwitchCell) ->OK.

 

Step 2

Select the Cross Platform template as Blank APP ->Set UI Technology as Forms and Sharing as PCL. Afterwards, Visual Studio creates 4 projects (Portable, Droid, iOS, UWP) and displays Getting Started.XamarinPage.

 

Step 3

In MainPage.Xaml page, add Label and SwitchCells in TableView.

 

  1. <StackLayout>  
  2.     <Label Text="Xamarin Form Switch Cell Demo in Android and UWP " VerticalOptions="Center" HorizontalOptions="Center" />  
  3.     <TableView Intent="Form" x:Name="SwiCellDemo" RowHeight="50">  
  4.         <TableRoot>  
  5.             <TableSection Title="Voice Mail">  
  6.                 <SwitchCell Text="New Voice Mail" /> </TableSection>  
  7.             <TableSection Title="Mail">  
  8.                 <SwitchCell Text="New Mail" On="True" /> </TableSection>  
  9.             <TableSection Title="Data">  
  10.                 <SwitchCell x:Name="SWCData" Text="Select Your Network" On="{Binding NetEn,Mode=TwoWay}" OnChanged="SWCData_OnOnChanged" /> </TableSection>  
  11.         </TableRoot>  
  12.     </TableView>  
  13.     <Picker x:Name="PicNetwork" HorizontalOptions="Fill" /> </StackLayout>  

 

Step 4

Add the following code in MainPage.Xaml.cs.

 

  1. public SwMobileBind TabSec;  
  2. TabSec = new SwMobileBind {  
  3.     NetEn = true  
  4. };  
  5. SwiCellDemo.BindingContext = TabSec;  
  6. private void SWCData_OnOnChanged(object sender, ToggledEventArgs e) {  
  7.     SelectNw(TabSec.NetEn);  
  8. }  
  9. private void SelectNw(bool snw) {  
  10.     PicNetwork.Items.Clear();  
  11.     if (snw) {  
  12.         PicNetwork.Title = "Mobile Data";  
  13.         PicNetwork.Items.Add("2G");  
  14.         PicNetwork.Items.Add("3G");  
  15.         PicNetwork.Items.Add("4G");  
  16.     } else {  
  17.         PicNetwork.Title = "WiFI";  
  18.         PicNetwork.Items.Add("Jio WiFi");  
  19.         PicNetwork.Items.Add("VVnetwotrk");  
  20.         PicNetwork.Items.Add("CrNet");  
  21.     }  
  22.     PicNetwork.SelectedIndex = 0;  
  23. }  
  24. }  
  25. public class SwMobileBind: INotifyPropertyChanged {  
  26.     private bool _NetEn;  
  27.     public bool NetEn {  
  28.         get {  
  29.             return _NetEn;  
  30.         }  
  31.         set {  
  32.             _NetEn = value;  
  33.             OnPropertyChanged();  
  34.         }  
  35.     }  
  36.     public event PropertyChangedEventHandler PropertyChanged;  
  37.     protected virtual void OnPropertyChanged([CallerMemberName] string propName = null) {  
  38.         PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propName));  
  39.     }  
  40. }  

 


Step 5

We will test Android and UWP. So, we can set the multiple startup projects as XamFormSwitchCell.Droid and XamFormSwitchCell.UWP (Universal Windows).

 

Step 6

Change the Configuration Manager settings. Go to Build -> Configuration Manager and uncheck the "Build" and "Deploy" options for iOS and check the options for Droid and UWP.

 

Step 7

Deploy your app on Android Emulator and Local Machine (UWP). The output of the XamFormSwitchCell app is shown below.

After clicking the Picker Control when Switch is ON status.

Now, the Switch is in Off State.

After clicking the Picker Control when Switch is in OFF status.

Summary

Now, you have successfully tested Switch Cells in Xamarin Forms application for Cross Platform Application Development using Visual C# and Xamarin.

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

TOP