Showing posts with label Xamarin.Forms. Show all posts

Introduction In this tutorial, we will learn how to create and use app shortcuts in the xamarin forms app using Xamar...

App Shortcuts in Xamarin Forms App Shortcuts in Xamarin Forms

A blog about android developement

Xamarin.Forms

App Shortcuts in Xamarin Forms

Introduction

In this tutorial, we will learn how to create and use app shortcuts in the xamarin forms app using Xamarin Essentials - AppActions class lets you create and respond to app shortcuts from the app icon. This will allow to navigate to the corresponding page from the app icon.


Coding Part

Steps

  1. Step 1: Creating new Xamarin.Forms Projects
  2. Step 2: Setting up the App Actions in Android and iOS
  3. Step 3: Implementation of App Shortcuts in Xamarin Forms

Step 1: Creating new Xamarin.Forms Projects

Create New Project by selecting "New Project" à "Xamarin Cross Platform App" and Clicking "OK".
Note: Xamarin.Forms version should be greater than 5.0.

Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

Step 2: Setting up the App Actions in Android and iOS

    In this step, we will see how to setup the code for Android and iOS.

    Android Setup
  • Add Intentfilter for App Action in your MainActivity.cs class. Refer to the below image.
  • Then add the following logic to handle actions:
  • iOS Setup
  • In the AppDelegate.cs add the following logic to handle actions:

Step 3: Implementation of App Shortcuts in Xamarin Forms

In this step, we will see how to implement the functionality in Xamarin.Forms.

Create App Actions in Xamarin.Forms
  • App Actions can be created at any time, but are often created when an application starts. Call the SetAsync method to create the list of actions for your app.
  • If App Actions are not supported on the specific version of the operating system a FeatureNotSupportedException will be thrown. The following properties can be set on an AppAction:
    1. Id: A unique identifier used to respond to the action tap.
    2. Title: The visible title to display.
    3. Subtitle: If supported a sub-title to display under the title.
    4. Icon: Must match icons in the corresponding resources directory on each platform.
  • When your application starts register for the OnAppAction event. When an app action is selected the event will be sent with information as to which action was selected.
  • Full Code

    App.xaml.cs AppDelegate.cs MainActivity.cs MainPage.xaml.cs

    Result

    Download Code:

    You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

    Introduction In this article, we will learn how to embed and use the barcode scanner inside the screen/page using the Xamarin.Forms Fram...

    Embedding QR Code Scanner in Xamarin.Forms Embedding QR Code Scanner in Xamarin.Forms

    A blog about android developement

    Xamarin.Forms

    Introduction

    In this article, we will learn how to embed and use the barcode scanner inside the screen/page using the Xamarin.Forms Framework. For achieving this, we are going to use “ZXing.Net.Mobile” plugin.

    Dropdown in Xamarin.Forms

    ZXing.Net.Mobile plugin is a useful plugin to facilitate scanning barcodes as effortless and painless as possible in our own applications works Xamarin.iOS, Xamarin.Android, Tizen, and UWP.

    Library Link: https://github.com/Redth/ZXing.Net.Mobile

    Without much introduction, we will skip into the coding part of this article.

    Coding Part

    Steps

    Step 1: Creating new Xamarin.Forms Projects.

    Step 2: Setting up the scanner in Xamarin.Forms .Net Standard Project

    Step 3: Implementation of QR Code Scanner inside the screen/page

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New à Project à Select Xamarin Cross Platform App and Click OK.

    Note: Xamarin.Forms version should be greater than 4.5.

    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Setting up the scanner in Xamarin.Forms .Net Standard Project

    In this step, we will see how to setup the plugin.

    • Open the Nuget Manager in the Visual Studio Solution by right click the solution and select “Manage Nuget Packages”.
    • Then “ZXing.Net.Mobile” and check all the projects in the solution, install the plugin
    • After the installation, we need to do some additional setup in the platform wise projects.
    • In Android, update the below code blocks in the MainActivity to initialize the plugin.
    // In OnCreate method
    Xamarin.Essentials.Platform.Init(Application);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
    
    // In Activity to handle the camera permission from the plugin it self.
    
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    Step 3: Implementation of QR Code Scanner inside the screen/page

    In this step, we will see how to use the view in Xamarin.Forms.

    • Open your designer file and in my case MainPage.xaml and add the ZxingSannerView as shown below.
      <zxing:ZXingScannerView x:Name="zxing"
                              VerticalOptions="FillAndExpand"/>
    • Add Label below the ZxingSannerView to see the results when the bar/QR code scanned.
    • Then add the below event to know the successful scan from the control.
      zxing.OnScanResult += (result) =>
            Device.BeginInvokeOnMainThread(() =>
            {
                  lblResult.Text = result.Text;
            });

    Full Code implementation of ZXing Scanner View in MainPage

    Here, we will see the full code for Main Page.

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            zxing.OnScanResult += (result) =>
                Device.BeginInvokeOnMainThread(() =>
                {
                    lblResult.Text = result.Text;
                });
        }
    
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            zxing.IsScanning = true;
        }
    
        protected override void OnDisappearing()
        {
            zxing.IsScanning = false;
            base.OnDisappearing();
        }
    }

    Demo

    The following screens shows the output this tutorial and it is awesome to have this dropdown in Xamarin.Forms

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.

    Introduction In this article, we will learn how to create Dropdown in Xamarin.Forms. By default, Android Platform has dropdown called as...

    Dropdown Control In Xamarin.Forms - Part One Dropdown Control In Xamarin.Forms - Part One

    A blog about android developement

    Xamarin.Forms



    Introduction

    In this article, we will learn how to create Dropdown in Xamarin.Forms. By default, Android Platform has dropdown called as “Spinner”, but iOS Platform has no dropdown like Android Spinner. In Xamarin.Forms, we have control called as Picker and we all heard about this. We are going to create a custom to achieve, the control like Android Spinner in Xamarin.Forms.

    Dropdown in Xamarin.Forms

    As per the introduction, we already having a dropdown control called as “Spinner” in Android Platform. Basically Xamarin.Forms control is a wrapper platform specific controls. For Example, Xamarin.Forms Entry is wrapper of EditText in Android and UITextField in iOS. We will go for View Renderer approach to have a new control to wrap a Platform specific control. Click the below link, to know more about view renderer.

    Reference

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view

    Without much introduction, we will skip into the coding part of this article.

    Coding Part:

    Steps:

    I have split this part into 3 steps as in the following.

    1. Creating new Xamarin.Forms Projects.
    2. Creating a Dropdown view in Xamarin.Forms .NetStandard Project.
    3. Wrapping Spinner for Dropdown control in Android Project.
    4. Implementation of Dropdown & It’s Demo for Android Platform.

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Creating a Dropdown view in Xamarin.Forms .NetStandard Project

    In this step, we will see how to create a dropdown view with required properties.

    1. Create a class named as “Dropdown” and inherit the Dropdown with “View”. That is Dropdown is child of View.
      public class Dropdown : View
      {
       //...
      }
    2. Then we will create a required bindable properties. First we will see, what are all the properties & events required for dropdown.
      1. ItemsSource – To assign the list of data to be populated in the dropdown.
      2. SelectedIndex – To identify the index of selected values from the ItemsSource.
      3. ItemSelected – Event for performing action when the item selected in the dropdown.
    3. Create a bindable property for the ItemsSource as shown in below.
      public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
       propertyName: nameof(ItemsSource),
       returnType: typeof(List<string>),
       declaringType: typeof(List<string>),
       defaultValue: null);
      
      public List<string> ItemsSource
      {
       get { return (List<string>)GetValue(ItemsSourceProperty); }
       set { SetValue(ItemsSourceProperty, value); }
      }
    4. Create a bindable property for the SelectedIndex as shown in below.
      public static readonly BindableProperty SelectedIndexProperty = BindableProperty.Create(
       propertyName: nameof(SelectedIndex),
       returnType: typeof(int),
       declaringType: typeof(int),
       defaultValue: -1);
      
      public int SelectedIndex
      {
       get { return (int)GetValue(SelectedIndexProperty); }
       set { SetValue(SelectedIndexProperty, value); }
      }
    5. Create a custom event ItemSelected for dropdown control and invoke the event as shown in below.
      public event EventHandler ItemSelected;
      
      public void OnItemSelected(int pos)
      {
       ItemSelected?.Invoke(this, new ItemSelectedEventArgs() { SelectedIndex = pos });
      }
    6. Here, ItemSelectedEventArgs is a child of EventArgs as shown in below.
      public class ItemSelectedEventArgs : EventArgs
      {
       public int SelectedIndex { get; set; }
      }
    Full Code of Dropdown View

    Here, we will see the full code for dropdown view.

    namespace XF.Ctrls
    {
        public class Dropdown : View
        {
            public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
                propertyName: nameof(ItemsSource),
                returnType: typeof(List<string>),
                declaringType: typeof(List<string>),
                defaultValue: null);
    
            public List<string> ItemsSource
            {
                get { return (List<string>)GetValue(ItemsSourceProperty); }
                set { SetValue(ItemsSourceProperty, value); }
            }
    
            public static readonly BindableProperty SelectedIndexProperty = BindableProperty.Create(
                propertyName: nameof(SelectedIndex),
                returnType: typeof(int),
                declaringType: typeof(int),
                defaultValue: -1);
    
            public int SelectedIndex
            {
                get { return (int)GetValue(SelectedIndexProperty); }
                set { SetValue(SelectedIndexProperty, value); }
            }
    
            public event EventHandler<ItemSelectedEventArgs> ItemSelected;
    
            public void OnItemSelected(int pos)
            {
                ItemSelected?.Invoke(this, new ItemSelectedEventArgs() { SelectedIndex = pos });
            }
        }
    
        public class ItemSelectedEventArgs : EventArgs
        {
            public int SelectedIndex { get; set; }
        }
    }

    Step 3: Wrapping Spinner for Dropdown control in Android Project.

    In this step, we will see “How to wrap the Android Spinner Control for Dropdown View”.

    1. Create a class file named as “DropdownRenderer” in your android client project and add View Renderer as shown in below.
      public class DropdownRenderer : ViewRenderer<Dropdown, Spinner>
      {
       Spinner spinner;
       public DropdownRenderer(Context context) : base(context)
       {
      
       } 
       // ...
      }
    2. Then Override the methods “OnElementChanged” and “OnElementPropertyChanged”. OnElementChanged method is triggered on element/control initiation. OnElementPropertyChanged method is called when the element property changes.
    3. Set Native Control to ViewRenderer using SetNativeControl() method in OnElementChanged override method as shown in below.
      protected override void OnElementChanged(ElementChangedEventArgs<Dropdown> e)
      {
       base.OnElementChanged(e);
      
       if (Control == null)
       {
        spinner = new Spinner(Context);
        SetNativeControl(spinner);
       }
       //...
      }
    4. Set Items Source from Xamarin.Forms Dropdown to Android Spinner control using array adapter as shown in below.
      var view = e.NewElement;
      
      ArrayAdapter adapter = new ArrayAdapter(Context, Android.Resource.Layout.SimpleListItem1, view.ItemsSource);
      Control.Adapter = adapter;
      
    5. Set default selection of item from selected index as shown in below.
      if (view.SelectedIndex != -1)
      {
       Control.SetSelection(view.SelectedIndex);
      }
    6. Create an item selected event for spinner and invoke the dropdown event created as shown below
      // ...
      Control.ItemSelected += OnItemSelected;
      // ...
      private void OnItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
      {
       var view = Element;
       if (view != null)
       {
        view.SelectedIndex = e.Position;
        view.OnItemSelected(e.Position);
       }
      }
    7. In the same way, we will assign ItemsSource & SelectedIndex to Android Spinner when the property changes using OnElementPropertyChanged as shown below.
      protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
      {
       var view = Element;
       if (e.PropertyName == Dropdown.ItemsSourceProperty.PropertyName)
       {
        ArrayAdapter adapter = new ArrayAdapter(Context, Android.Resource.Layout.SimpleListItem1, view.ItemsSource);
        Control.Adapter = adapter;
       }
       if (e.PropertyName == Dropdown.SelectedIndexProperty.PropertyName)
       {
        Control.SetSelection(view.SelectedIndex);
       }
       base.OnElementPropertyChanged(sender, e);
      }
    8. Add Export Renderer above the namespace to link dropdown view in .NetStandard project to Android Client Project. This is very important step for any custom renderer approach.
      [assembly: ExportRenderer(typeof(Dropdown), typeof(DropdownRenderer))]
      namespace XF.Ctrls.Droid
    Full Code of Dropdown Renderer

    Here, we will see the full code for Dropdown Renderer.

    [assembly: ExportRenderer(typeof(Dropdown), typeof(DropdownRenderer))]
    namespace XF.Ctrls.Droid
    {
        public class DropdownRenderer : ViewRenderer<Dropdown, Spinner>
        {
            Spinner spinner;
            public DropdownRenderer(Context context) : base(context)
            {
    
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Dropdown> e)
            {
                base.OnElementChanged(e);
    
                if (Control == null)
                {
                    spinner = new Spinner(Context);
                    SetNativeControl(spinner);
                }
    
                if (e.OldElement != null)
                {
                    Control.ItemSelected -= OnItemSelected;
                }
                if (e.NewElement != null)
                {
                    var view = e.NewElement;
    
                    ArrayAdapter adapter = new ArrayAdapter(Context, Android.Resource.Layout.SimpleListItem1, view.ItemsSource);
                    Control.Adapter = adapter;
    
                    if (view.SelectedIndex != -1)
                    {
                        Control.SetSelection(view.SelectedIndex);
                    }
    
                    Control.ItemSelected += OnItemSelected;
                }
            }
    
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                var view = Element;
                if (e.PropertyName == Dropdown.ItemsSourceProperty.PropertyName)
                {
                    ArrayAdapter adapter = new ArrayAdapter(Context, Android.Resource.Layout.SimpleListItem1, view.ItemsSource);
                    Control.Adapter = adapter;
                }
                if (e.PropertyName == Dropdown.SelectedIndexProperty.PropertyName)
                {
                    Control.SetSelection(view.SelectedIndex);
                }
                base.OnElementPropertyChanged(sender, e);
            }
    
            private void OnItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                var view = Element;
                if (view != null)
                {
                    view.SelectedIndex = e.Position;
                    view.OnItemSelected(e.Position);
                }
            }
        }
    }

    Step 4: Implementation of Dropdown & It’s Demo for Android Platform

    In this step, we will see how to use the view in Xamarin.Forms.

    1. Open your designer file and in my case MainPage.xaml and add the control as shown below.
      <local:Dropdown HorizontalOptions="FillAndExpand"
            VerticalOptions="Center"
            BackgroundColor="LawnGreen"
            x:Name="dropdown"/>
    2. Set ItemsSource and SelectedIndex as shown in below.
      dropdown.ItemsSource = Items1;
      dropdown.SelectedIndex = 1;
    3. Add item selection event to dropdown as shown in below.
      public MainPage()
      {
       //...
       dropdown.ItemSelected += OnDropdownSelected;
      }
      
      private void OnDropdownSelected(object sender, ItemSelectedEventArgs e)
      {
       label.Text = Items1[e.SelectedIndex];
      }
    Full Code implementation of Dropdown in MainPage

    Here, we will see the full code for Main Page.

    namespace XF.Ctrls
    {
        public partial class MainPage : ContentPage
        {
            List<string> Items1 = new List<string>();
            List<string> Items2 = new List<string>();
            bool IsItem1 = true;
    
            public MainPage()
            {
                InitializeComponent();
    
                for (int i = 0; i < 4; i++)
                {
                    Items1.Add(i.ToString());
                }
    
                for (int i = 0; i < 10; i++)
                {
                    Items2.Add(i.ToString());
                }
    
                dropdown.ItemsSource = Items1;
                dropdown.SelectedIndex = 1;
                dropdown.ItemSelected += OnDropdownSelected;
            }
    
            private void OnDropdownSelected(object sender, ItemSelectedEventArgs e)
            {
                label.Text = IsItem1 ? Items1[e.SelectedIndex] : Items2[e.SelectedIndex];
            }
    
            private void btn_Clicked(object sender, EventArgs e)
            {
                dropdown.ItemsSource = IsItem1 ? Items2 : Items1;
                dropdown.SelectedIndex = IsItem1 ? 5 : 1;
                IsItem1 = !IsItem1;
            }
        }
    }

    Demo

    The following screens shows the output this tutorial and it is awesome to have this dropdown in Xamarin.Forms.

    This article covers the implementation of new dropdown control in Android Platform alone. Currently, I am working on creating spinner like dropdown control in iOS Platform and will post the article about the implementation of the dropdown in iOS Platform soon.

    My plan is to create a dropdown control for supporting all platforms and offering this control as a standard plugin.

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.


    Introduction: In this tutorial, we will learn how to use FlowListView in Xamarin.Forms to create GridView. FlowListView is an awesome p...

    Grid View in Xamarin.Forms using FlowListView Grid View in Xamarin.Forms using FlowListView

    A blog about android developement

    Xamarin.Forms


    Introduction:

    In this tutorial, we will learn how to use FlowListView in Xamarin.Forms to create GridView. FlowListView is an awesome plugin facilitates developer to achieve features like infinite loading, Item Tapped Command, Item appearing event, item disappearing event and more.

    Coding Part:

    Steps:

    I have split this part into 3 steps as in the following.

    1. Creating new Xamarin.Forms Projects.
    2. Setting up the plugin for Xamarin.Forms Application.
    3. Implementing GridView with FlowListView.

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Setting up the plugin for Xamarin.Forms Application

    Open Nuget Package Manager against the solution and do search for FlowListView Plugin or Paste the following Nuget package.
    Install-Package DLToolkit.Forms.Controls.FlowListView -Version 2.0.11

    Click Install to install this Plugin against your PCL Project or .NET standard Project.

    We need to install this application in all client projects.

    Step 3: Implementing GridView with FlowListView

    1. Open “App.xaml.cs” or “App.cs” and add the following line after InitializeComponent function.
      public App()
      {
         InitializeComponent();
         FlowListView.Init();
         MainPage = new MainPage();
      }
    2. Open your Page for example “MainPage” and add the flowlistview reference in designer as like below.
      …
      xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
      …
    3. Implement the flowlistview like below.
      <flv:FlowListView FlowColumnCount="3" 
                      SeparatorVisibility="Default" 
                      HasUnevenRows="True"
                      FlowItemTappedCommand="{Binding ItemTappedCommand}" 
                      FlowItemsSource="{Binding Items}">
      
          <flv:FlowListView.FlowColumnTemplate>
              <DataTemplate>
                  <Frame BackgroundColor="Purple"
                      Margin="5">
                      <Label HorizontalOptions="Fill" 
                          VerticalOptions="Fill" 
                          TextColor="White"
                          XAlign="Center"
                          YAlign="Center" 
                          Text="{Binding }"/>
                  </Frame>
              </DataTemplate>
          </flv:FlowListView.FlowColumnTemplate>
      </flv:FlowListView>
    4. Then create a ViewModel for your Page and in my case I have created a class named as “MainPageModel.cs” and inherits the class with BindableObject.
      public class MainPageModel : BindableObject
      {
       …
      }
    5. Then add the view model to your page like below
      public partial class MainPage : ContentPage
      {
          MainPageModel pageModel;
          public MainPage()
          {
              InitializeComponent();
              pageModel = new MainPageModel(this);
              BindingContext = pageModel;
          }
      }

    Full Code:

    MainPage.xaml
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
                 x:Class="FlowListViewSample.MainPage">
    
        <StackLayout Padding="10">
            <flv:FlowListView FlowColumnCount="3" 
                    SeparatorVisibility="Default" 
                    HasUnevenRows="True"
                    FlowItemTappedCommand="{Binding ItemTappedCommand}" 
                    FlowItemsSource="{Binding Items}">
    
                <flv:FlowListView.FlowColumnTemplate>
                    <DataTemplate>
                        <Frame BackgroundColor="Purple"
                    Margin="5">
                            <Label HorizontalOptions="Fill" 
                        VerticalOptions="Fill" 
                        TextColor="White"
                        XAlign="Center"
                        YAlign="Center" 
                        Text="{Binding }"/>
                        </Frame>
                    </DataTemplate>
                </flv:FlowListView.FlowColumnTemplate>
            </flv:FlowListView>
        </StackLayout>
    
    </ContentPage>
    MainPage.xaml.cs
    using Xamarin.Forms;
    
    namespace FlowListViewSample
    {
        public partial class MainPage : ContentPage
        {
            MainPageModel pageModel;
            public MainPage()
            {
                InitializeComponent();
                pageModel = new MainPageModel(this);
                BindingContext = pageModel;
            }
        }
    }
    MainPageModel.cs
    using System.Collections.ObjectModel;
    using Xamarin.Forms;
    
    namespace FlowListViewSample
    {
        public class MainPageModel : BindableObject
        {
            private MainPage mainPage;
    
            public MainPageModel(MainPage mainPage)
            {
                this.mainPage = mainPage;
                AddItems();
            }
    
            private void AddItems()
            {
                for (int i = 0; i < 20; i++)
                    Items.Add(string.Format("List Item at {0}", i));
            }
    
            private ObservableCollection _items = new ObservableCollection();
            public ObservableCollection Items
            {
                get
                {
                    return _items;
                }
                set
                {
                    if (_items != value)
                    {
                        _items = value;
                        OnPropertyChanged(nameof(Items));
                    }
                }
            }
    
            public Command ItemTappedCommand
            {
                get
                {
                    return new Command((data) =>
                    {
                        mainPage.DisplayAlert("FlowListView", data + "", "Ok");
                    });
                }
            }
        }
    }

    Demo:

    Reference

    FlowListView for Xamarin.Forms https://github.com/daniel-luberda/DLToolkit.Forms.Controls/tree/master/FlowListView/

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.

    Introduction: In this tutorial, we will learn how to use Rg.Plugin.Popup in Xamarin.Forms using Fresh MMVM. In my previous articles, we h...

    Rg Popup in Xamarin.Forms using Fresh MVVM Rg Popup in Xamarin.Forms using Fresh MVVM

    A blog about android developement

    Xamarin.Forms

    Introduction:

    In this tutorial, we will learn how to use Rg.Plugin.Popup in Xamarin.Forms using Fresh MMVM. In my previous articles, we have learned how to use Fresh MVVM with Navigation Page, Master Detail Page and Tabbed Page. If you are new to Fresh MVVM, kindly read my previous articles on Fresh MVVM to know the basics & rules of Fresh MVVM.

    Coding Part:

    Steps:

    I have split this part into 3 steps as in the following.

    1. Creating new Xamarin.Forms Projects.
    2. Setting up the plugin for Xamarin.Forms Application.
    3. Implementing Fresh MVVM.

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Setting up the plugin for Xamarin.Forms Application

    We will start coding for Fresh MVVM. Create New Xamarin Forms Project. Open Nuget Package Manager against the solution and do search for Fresh MVVM Plugin or Paste the following Nuget Installation.
    Install-Package FreshMvvm -Version 2.2.3

    Click Install to install this Plugin against your PCL Project or .NET standard Project. Then download the Rg.Plugin.Popup by using the following Nuget package.

    Install-Package Rg.Plugins.Popup -Version 1.1.5.188

    Click Install to install this Plugin against your PCL Project or .NET standard Project and all dependent platform projects.

    Step 3: Implementing Fresh MVVM Page & Page Models

    1. Create your XAML page (view) with name ended up with “Page”.
    2. Create PageModel by create Class name ended with “PageModel” and inherited with “FreshBasePageModel” as shown below screenshot.
    3. I have created a Page & PageModel named as “MainPage” & “MainPageModel” and set this page as Main Page/Root Page of the application as shown in the following.

      Set MainPage:

      We need to set the MainPageModel as MainPage using FreshNavigationConatiner. Open App.xaml.cs or App.cs and set MainPage.

      // To set MainPage for the Application
      var page = FreshPageModelResolver.ResolvePageModel();
      var basicNavContainer = new FreshNavigationContainer(page);
      MainPage = basicNavContainer;

    4. 4. Then create a Popup Page using Rg.Plugins.Popup by adding “”. The following code snippet shows how to create popup using Rg.Plugin. I have created a Xamarin.Forms Page and named as “MyPopupPage.xaml”.
      <?xml version="1.0" encoding="utf-8" ?>
      <popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
                       CloseWhenBackgroundIsClicked="True"
                       x:Class="Popup.MyPopupPage">
          <popup:PopupPage.Content>
              <StackLayout Padding="10"
                           BackgroundColor="White"
                           HorizontalOptions="Center"
                           VerticalOptions="Center">
                  <Label Text="Fresh MVVM Rg.Plugin.Popup"
                      VerticalOptions="CenterAndExpand" 
                      HorizontalOptions="CenterAndExpand" />
                  <Button Command="{Binding ClosePopupCommand}"
                          Text="Close Popup"/>
              </StackLayout>
          </popup:PopupPage.Content>
      </popup:PopupPage>
    5. The code behind has “PopupPage” as parent page like shown in the following code part.
      public partial class MyPopupPage : PopupPage
      {
       public MyPopupPage ()
       {
        InitializeComponent ();
       }
      }
    6. Then create a Page Model for the created Popup Page with Fresh MVVM rules. If you have not remember the rules or new to Fresh MVVM, Kindly refer my previous articles on fresh MVVM.
      public class MyPopupPageModel : FreshBasePageModel
      {
      
      }
    7. Rg Popup Page has a separate Navigation Stack. So, open and close the popup, we need to have separate Navigation Stack. To know more about Rg.Plugin.Popup, refer the GitHub Link.
      Rg.Plugin.Popup Link: https://github.com/rotorgames/Rg.Plugins.Popup
    8. Then include Fresh MVVM extension/Utility class to your Xamarin Shared Project. This extension file is created & open sourced by the author “Libin Joseph”. You can download this file from the following GitHub Link.
      Fresh MVVM Extension Link: https://github.com/libin85/FreshMvvm.Popup
    9. To open popup page like navigating to normal Fresh MVVM page, use the following code snippet.
      return new Command(async () =>
      {
              await CoreMethods.PushPopupPageModel();
      });
    10. To close the popup page like closing normal Fresh MVVM page, use the following code snippet.
      return new Command(async () =>
      {
              await CoreMethods.PopPopupPageModel();
      });

    Demo:

    The below screenshots for your reference.

    Main PageRg Popup Page

    Reference:

    Fresh MVVMhttps://github.com/rid00z/FreshMvvm
    Rg.Plugin.Popuphttps://github.com/rotorgames/Rg.Plugins.Popup
    Fresh MVVM Popup Extensionhttps://github.com/libin85/FreshMvvm.Popup/

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.

    Introduction: In this tutorial, we will learn how to use master detail page using Fresh MVVM with FreshNavigationContainer. It has some w...

    Custom Master Detail Page in Xamarin.Forms using Fresh MVVM Custom Master Detail Page in Xamarin.Forms using Fresh MVVM

    A blog about android developement

    Xamarin.Forms

    Introduction:

    In this tutorial, we will learn how to use master detail page using Fresh MVVM with FreshNavigationContainer. It has some work around for creating master detail page more than creating master detail page using FreshMasterContainer. In my previous tutorials, we have learned the method to create master detail page using Fresh Master Container. To know more, visit my previous article.

    Kindly refer my previous article on Fresh MVVM to know the basics & rules of Fresh MVVM.

    Coding Part:

    Steps:

    I have split this part into 3 steps as in the following.

    1. Creating new Xamarin.Forms Projects.
    2. Setting up the plugin for Xamarin.Forms Application.
    3. Implementing Fresh MVVM.

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Setting up the plugin for Xamarin.Forms Application

    We will start coding for Fresh MVVM. Create New Xamarin Forms Project. Open Nuget Package Manager against the solution and do search for Fresh MVVM Plugin or Paste the following Nuget Installation.
    Install-Package FreshMvvm -Version 2.2.3
    Click Install to install this Plugin against your PCL Project or .NET standard Project.

    Step 3: Implementing Fresh MVVM.

    1. Create your XAML page (view) with name ended up with “Page”.
    2. Create PageModel by create Class name ended with “PageModel” and inherited with “FreshBasePageModel” as shown below screenshot.

    In the same way, I have created pages “MyMasterDetailPage”, “MasterPage”, “Detail1Page” and “Detail2Page” with two respective models “MyMasterDetailPageModel”, “MasterPageModel”, “Detail1PageModel” and “Detail2PageModel”.

    Here,
    MyMasterDetailPagecontainer to have both Master & Detail page
    MasterPageMaster or Menu Page
    Detail1Page, Detail2PageDetail pages of the Master detail page

    Set MainPage:

    We need to set the MainPageModel as MainPage using FreshNavigationConatiner.

    1. Open App.xaml.cs or App.cs and set MainPage.
      // To set MainPage for the Application
      var page = FreshPageModelResolver.ResolvePageModel();
      var basicNavContainer = new FreshNavigationContainer(page);
      MainPage = basicNavContainer;
    2. Then add button with command for opening the MasterDetailPage from MainPage.
      public Command GotoSecondPageCommand
      {
          get
          {
              return new Command(async () =>
              {
                  await CoreMethods.PushPageModel();
              });
          }
      }
    3. Open MyMasterDetailPage and add Master, Detail page to the constructor of the page like below.
      public MyMasterDetailPage()
      {
          InitializeComponent();
      
          NavigationPage.SetHasNavigationBar(this, false);
      
          Master = FreshPageModelResolver.ResolvePageModel();
          Detail = new NavigationPage(FreshPageModelResolver.ResolvePageModel());
      }
    4. Then click “Run” to see your Custom Master Detail Page using FreshMVVM with FreshNavigationContainer.

    By this way, we can have Navigation Service & Stack for all type of Pages. Kindly find the screenshot for your reference.

    MainPageMaster Page(Detail)Master Page(Master)

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.

    Introduction: In this tutorial, we will learn how create Tabbed Page in Xamarin.Forms using Fresh MVVM. We already learned how to create ...

    Tabbed Page in Xamarin.Forms using Fresh MVVM Tabbed Page in Xamarin.Forms using Fresh MVVM

    A blog about android developement

    Xamarin.Forms



    Introduction:

    In this tutorial, we will learn how create Tabbed Page in Xamarin.Forms using Fresh MVVM. We already learned how to create your master details page in my previous tutorials. If you are new to Fresh MVVM, You can read my previous article here.

    Articles of FreshMVVM:

    1. MVVM Databinding in Xamarin.Forms using Fresh MVVM.
    2. Master Detail Page in Xamarin.Forms using Fresh MVVM.

    Coding Part:

    Steps:
    I have split this part into 3 steps as in the following.
    1. Creating new Xamarin.Forms Projects.
    2. Setting up the plugin for Xamarin.Forms Application.
    3. Implementing Fresh MVVM Tabbed Page.

    Step 1: Creating new Xamarin.Forms Projects

    Create New Project by Selecting New -> Project -> Select Xamarin Cross Platform App and Click OK.
    Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

    Step 2: Setting up the plugin for Xamarin.Forms Application

    We will start coding for Fresh MVVM. Create New Xamarin Forms Project. Open Nuget Package Manager against the solution and do search for Fresh MVVM Plugin or Paste the following Nuget Installation.
    Install-Package FreshMvvm -Version 2.2.3
    Click Install to install this Plugin against your PCL Project or .NET standard Project.

    Step 3: Implementing Fresh MVVM Tabbed Page

    1. Create your XAML page (view) with name ended up with “Page”.
    2. Create PageModel by create Class name ended with “PageModel” and inherited with “FreshBasePageModel” as shown below screenshot.
    3. In the same way, I have created two pages “Detail1Page” and “Detail2Page” with two respective page models “Detail1PageModel” and “Detail2PageModel”.
    Set MainPage:
    To create Fresh MVVM Tabbed Page as MainPage, we should use Fresh Tabbed Navigation Container with the following code. Open App.xaml.cs or App.cs and set MainPage.
    var tabbedNavigation = new FreshTabbedNavigationContainer();
    tabbedNavigation.AddTab("First Tab", null);
    tabbedNavigation.AddTab("Second Tab", null);
    MainPage = tabbedNavigation;
    
    • Then Click Run, your Tabbed Page will be look like as on the below screenshot.
    • Here, we will not discuss about navigation between pages. If you want to know, you can see my previous article on Fresh MVVM.
    Full Code for App.xaml.cs:
    You can find the code for App.xaml.cs below
    public partial class App : Application
    {
        public App()
        {
            try
            {
                InitializeComponent();
                var tabbedNavigation = new FreshTabbedNavigationContainer();
               tabbedNavigation.AddTab("First Tab", null);
               tabbedNavigation.AddTab("Second Tab", null);
               MainPage = tabbedNavigation;
            }
            catch (Exception ex)
            {
    
            }
        }
    
        protected override void OnStart()
        {
            // Handle when your app starts
        }
    
        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }
    
        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

    Download Code

    You can download the code from GitHub. If you have doubt, feel free to post comment. If you like this article and is useful to you, do like, share the article & star the repository on GitHub.