Let's start.

Step 1

Open Visual Studio.
Go to File >> New>> Project.
Select Templates >> Visual C#>> Cross Platform.
Select Cross Platform App (Xamarin.Forms or Native) and give your application a suitable name, such as - Instagram.
Now, choose project location and click OK.



Step 2

Create Models folder; For that, go to Solution Explorer >> right click on Instagram (PCL) app >> Add >> New Folder. Give the folder name such as Models.
Create a class named InstagramItems inside the Models folder. For that, go to Solution Explorer >> Instagram(PCL) >> right click and select new item. In the popup window, select Cross platform >> class. Give the class name Instagramitem. Double click to get into its design view and insert the code given below.

  1. namespace XamarinForms.Models
  2. {
  3. public class InstagramItem
  4. {
  5. public string UserName { get; set; }
  6. public string FullName { get; set; }
  7. public string ProfilePicture { get; set; }
  8. public string LowResolutionUrl { get; set; }
  9. public string ThumbnailUrl { get; set; }
  10. public string StandardResolutionUrl { get; set; }
  11. public string Text { get; set; }
  12. public string CreatedTime { get; set; }
  13. public int LikesCount { get; set; }object
  14. public int CommentsCount { get; set; }
  15. }
  16. }

Step 3

Similarly, create a Views folder and Instagramviewpage class inside it. Here is the code for this class. This page is your application UI.

  1. using Xamarin.Forms;
  2. using XamarinForms.ViewModels;
  3. namespace XamarinForms.Views
  4. {
  5. public class InstagramViewPage : ContentPage
  6. {
  7. public InstagramViewPage()
  8. {
  9. BackgroundColor = Color.White;
  10. var twitterViewModel = new InstagramViewModel();
  11. BindingContext = twitterViewModel;
  12. var label = new Label
  13. {
  14. Text = "Instagram",
  15. TextColor = Color.Gray,
  16. FontSize = 24
  17. };
  18. var dataTemplate = new DataTemplate(() =>
  19. {
  20. var screenNameLabel = new Label
  21. {
  22. TextColor = Color.FromHex("#1c5380"),
  23. FontSize = 22
  24. };
  25. var textLabel = new Label
  26. {
  27. TextColor = Color.Black,
  28. FontSize = 14
  29. };
  30. var likesLabel = new Label
  31. {
  32. TextColor = Color.FromHex("#517fa4"),
  33. FontSize = 14
  34. };
  35. var commentsLabel = new Label
  36. {
  37. TextColor = Color.FromHex("#517fa4"),
  38. FontSize = 14
  39. };
  40. var profileImage = new Image
  41. {
  42. WidthRequest = 60,
  43. HeightRequest = 60,
  44. VerticalOptions = LayoutOptions.Start
  45. };
  46. var mediaImage = new Image
  47. {
  48. HeightRequest = 200
  49. };
  50. screenNameLabel.SetBinding(Label.TextProperty, new Binding("UserName"));
  51. textLabel.SetBinding(Label.TextProperty, new Binding("Text"));
  52. profileImage.SetBinding(Image.SourceProperty, new Binding("ProfilePicture"));
  53. mediaImage.SetBinding(Image.SourceProperty, new Binding("StandardResolutionUrl"));
  54. likesLabel.SetBinding(Label.TextProperty, new Binding("LikesCount", BindingMode.Default, null, null, "{0:n0} likes |"));
  55. commentsLabel.SetBinding(Label.TextProperty, new Binding("CommentsCount", BindingMode.Default, null, null, "{0:n0} likes"));
  56. return new ViewCell
  57. {
  58. View = new StackLayout
  59. {
  60. Orientation = StackOrientation.Horizontal,
  61. Padding = new Thickness(0, 5),
  62. Children =
  63. {
  64. profileImage,
  65. new StackLayout
  66. {
  67. Orientation = StackOrientation.Vertical,
  68. Children =
  69. {
  70. screenNameLabel,
  71. new StackLayout
  72. {
  73. Orientation = StackOrientation.Horizontal,
  74. Children =
  75. {
  76. likesLabel,
  77. commentsLabel,
  78. }
  79. },
  80. textLabel,
  81. mediaImage,
  82. }
  83. }
  84. }
  85. }
  86. };
  87. });
  88. var listView = new ListView
  89. {
  90. HasUnevenRows = true
  91. };
  92. listView.SetBinding(ListView.ItemsSourceProperty, "InstagramItems");
  93. listView.ItemTemplate = dataTemplate;
  94. Content = new StackLayout
  95. {
  96. Padding = new Thickness(5, 10),
  97. Children =
  98. {
  99. label,
  100. listView
  101. }
  102. };
  103. }
  104. }
  105. }
Step 4.1

Create another folder named ViewModels and create InstagramViewModels class inside it. Here is the code.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Net.Http;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading.Tasks;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using XamarinForms.Models;
  11. namespace XamarinForms.ViewModels
  12. {
  13. public class InstagramViewModel : INotifyPropertyChanged
  14. {
  15. private List<InstagramItem> _instagramItems;
  16. public List<InstagramItem> InstagramItems
  17. {
  18. get { return _instagramItems; }
  19. set
  20. {
  21. _instagramItems = value;
  22. OnPropertyChanged();
  23. }
  24. }
  25. public InstagramViewModel()
  26. {
  27. InitDataAsync();
  28. }
  29. private async Task InitDataAsync()
  30. {
  31. var httpClient = new HttpClient();
  32. var json = await httpClient.GetStringAsync(
  33. "https://www.instagram.com/xamarinhq/media/"
  34. //"https://api.instagram.com/v1/users/469744406/media/recent?client_id=88a92ed981d74235a5a2be3cf549cb59"
  35. );
  36. JObject response = JsonConvert.DeserializeObject<dynamic>(json);
  37. var items = response.Value<JArray>("items");
  38. try
  39. {
  40. var instagramItems = new List<InstagramItem>();
  41. foreach (var item in items)
  42. {
  43. var instagramItem = new InstagramItem
  44. {
  45. UserName = item.Value<JObject>("user").Value<string>("username"),
  46. FullName = item.Value<JObject>("user").Value<string>("full_name"),
  47. ProfilePicture = item.Value<JObject>("user").Value<string>("profile_picture"),
  48. LowResolutionUrl = item.Value<JObject>("images").Value<JObject>("low_resolution").Value<string>("url"),
  49. StandardResolutionUrl = item.Value<JObject>("images").Value<JObject>("standard_resolution").Value<string>("url"),
  50. ThumbnailUrl = item.Value<JObject>("images").Value<JObject>("thumbnail").Value<string>("url"),
  51. Text = item.Value<JObject>("caption").Value<string>("text"),
  52. CreatedTime = item.Value<JObject>("caption").Value<string>("created_time"),
  53. LikesCount = item.Value<JObject>("likes").Value<int>("count"),
  54. CommentsCount = item.Value<JObject>("comments").Value<int>("count"),
  55. };
  56. instagramItems.Add(instagramItem);
  57. }
  58. InstagramItems = instagramItems;
  59. }
  60. catch (Exception exception)
  61. {
  62. }
  63. }
  64. public event PropertyChangedEventHandler PropertyChanged;
  65. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  66. {
  67. if (PropertyChanged != null)
  68. {
  69. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  70. }
  71. }
  72. }
  73. }
Step 4.2

Now, we need to add namespaces. For that, go to Solution Explorer >> Instagram(PCL) >> click open InstagramViewModels page design view and add the following namespaces.

  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Net.Http;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using Instagram.Models;
  9. using XamarinForms.Models;
Step 5

Add References now. For that, go to Solution Explorer >> Instagram(PCL)app >> right click on References >> Add References. Add the given references.

  1. Newtonsoft.Json
  2. System.Net.Http
  3. System.Net.Http.Extensions
  4. System.Net.Http.Primitivies
Step 6

After design view creation, go to Solution Explorer >> Instagram(PCL) >> click open App.xaml.cs file and declare that the Main page is InstagramViewPage. The code is given below.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xamarin.Forms;
  6. using XamarinForms.Views;
  7. using XamarinForms.Models;
  8. using XamarinForms.ViewModels;
  9. namespace Instagram
  10. {
  11. public partial class App : Application
  12. {
  13. public App()
  14. {
  15. InitializeComponent();
  16. MainPage = new InstagramViewPage();
  17. }
  18. protected override void OnStart()
  19. {
  20. // Handle when your app starts
  21. }
  22. protected override void OnSleep()
  23. {
  24. // Handle when your app sleeps
  25. }
  26. protected override void OnResume()
  27. {
  28. // Handle when your app resumes
  29. }
  30. }
  31. }
Step 7

Now, go to Build menu and click "Configure Manager". Here, you can configure your startup projects. Click F5 or start Build and Run your application.

Finally, we have successfully created a Xamarin.Forms Instagram application.
Conclusion

I hope you have learned how to create an Instagram application using MVVM pattern.