Achindra Bhatnagar
Published © MIT

Xamarin, Sharing is Good

Xamarin saves a lot of programming, testing and maintenance effort by using a shared codebase. You write code, find bugs and fix them!

IntermediateProtip1 hour298
Xamarin, Sharing is Good

Things used in this project

Hardware components

Android device
Android device
×1
iPhone
Apple iPhone
×1
Microsoft Windows Phone 10
×1

Software apps and online services

Xamarin
Xamarin
Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Code

Code snippet #1

Plain text
// Shared Project
public class NotesApp
{
   void SaveFile()
   {
#if __IOS__
       new UIAlertView("File Save",
                       "File saved to cloud drive",
                       null,
                       "OK").Show();
#endif
   }
}

Code snippet #2

Plain text
// Shared Project
public class NotesApp
{
    void SaveFile()
    {
        Message.Show("File Save", "File saved to cloud drive");
    }
}

// iOS Project
internal class Message()
{
    internal static void Show(string head, string msg)
    {
        new UIAlertView(head, message, null, "OK").Show();
    }
}

// Android Project
 internal class Message() 
{
   internal static void Show(string head, string msg)
   {
       Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(this);  
       AlertDialog alert = dialog.Create();  
       alert.SetTitle(head);  
       alert.SetMessage("msg");  
       alert.SetButton("OK", (c, ev) =>  
       {  
           // 'OK' click action
       });  
       alert.Show();
   }
}

Code snippet #3

Plain text
// Shared Project
partial class NotesApp
{
   partial void Show(string head, string msg);

   void OnSaveFile()
   {
       Show("File Save", "File saved to cloud drive");
   }
}

// iOS Project
partial class NotesApp()
{
   void Show(string head, string msg)
   {
       new UIAlertView(head, message, null, "OK").Show();
   }
}

// Android Project
partial class NotesApp()
{
   // Method not defined
}

Code snippet #4

Plain text
//
//Conditional Compilation Sample
//

// Sample Shared Code

public class Phone
 {
 public string Name { get; set; }
 public long Number { get; set; }
 public string Address { get; set; }
 }

public static class PhoneDirectory   
{
      const string YelloPages = "YellowPages.json";

      public static async Task<IEnumerable<Phone>> Load()
      {
         using (var reader = new StreamReader(await OpenFile()))
         {
            return JsonConvert.DeserializeObject<List<Phone>>(await reader.ReadToEndAsync());
         }
      }
      private async static Task<Stream> OpenFile()
      {
#if __ANDROID__
         return Android.App.Application.Context.Assets.Open(YelloPages);
#endif
#if __IOS__
         return File.OpenRead(YelloPages);
#endif
#if WINDOWS_UWP
         var fileStream = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(YelloPages);
         return await fileStream.OpenStreamForReadAsync();
#endif
         return null;
      }
   }


// Sample iOS code
public override async void ViewDidLoad()
{
   base.ViewDidLoad();
   var data = await PhoneDirectory.Load();
   TableView.Source = new ViewControllerSource<Phone>(TableView)
   {
      DataSource = data.ToList(),
      TextProc = s => s.Name,
      DetailTextProc = s => s.Number + "-" + s.Address,
   };
}

// Sample Android code
protected override async void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);
   var data = await PhoneDirectory.Load();
   ListAdapter = new ListAdapter<Phone>()
   {
     DataSource = data.ToList(),
     TextProc = s => s.Name,
     DetailTextProc = s => s.Number + "-" + s.Address
   };
 }

// Sample UWP Code
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
   DataContext = await PhoneDirectory.Load();
}


//
// * if you are wondering what is DataContext?,
//      DataContext is the default source of bindings in WPF
//      We are populating that in our code and UI refreshes on its own
//

Credits

Achindra Bhatnagar

Achindra Bhatnagar

15 projects • 113 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer
Contact

Comments

Add projectSign up / Login