Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Get counrty code picker or popup

Hello guy's

my question is , that is there any plugin for get all country code for telephone in xamarin forms.
i'm surfing in google and get one thing CCP (country code picker ) for native android but for native ios i don't found any option,
so if nay solution in xamarin forms for this task please shear it .

See this link:- https://stackoverflow.com/questions/21421313/creating-a-spinner-for-choosing-country-code

Thank's.

Posts

  • Ashish_sharmaAshish_sharma INMember ✭✭✭

    Hello guy's
    we have done this task with my friend help.
    Let start

    1) Create a countrycode.txt file in pcl. and past json formate text of countrycodelist into it.https://drive.google.com/file/d/1Dk8pRUpDo1UEKeU1o5sKnwtkKxSbw0g7/view?usp=sharing

    2).Create model class

    public class CountryCodeModel
    {
    public ObservableCollection countries { get; set; }
    }
    public class Country
    {
    public string code { get; set; }
    public string name { get; set; }
    public string codevalue { get; set; }

    }
    

    3) Create xaml Page



    <ListView.ItemTemplate>




    Note:- On xaml .cs page put

    private CountryCodeViewModel ccVM;
    BindingContext = ccVM = new CountryCodeViewModel();

    4). Create View Model

    public CountryCodeViewModel()
    {
    restapi = new RestServices();
    CountryCodeList = new ObservableCollection();
    Getcountrycodemethod();
    }
    private ObservableCollection _countrycode;
    public ObservableCollection CountryCodeList
    {
    get { return _countrycode; }
    set
    {
    _countrycode = value;
    OnPropertyChanged("CountryCodeList");
    }
    }

        public  void Getcountrycodemethod()
        {
            try
            {
                var result = restapi.GetCountryCodeList();
                CountryCodeList = result.countries;
            }
            catch (Exception EX)
            {
            } 
        }
    }
    

    5). Create RestApi Class and put this method in this class.

    using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Net.Http;
    using TravelApp.Models;

    public CountryCodeModel GetCountryCodeList()
    {
    try
    {
    string json = ReadTextFile();
    var list = JsonConvert.DeserializeObject(json);
    return list;
    }
    catch (Exception ex)
    {
    string x = ex.Message;
    return null;
    }
    }

        public string ReadTextFile()
        {
            try
            {
                string text = "";
                Stream stream = this.GetType().Assembly.GetManifestResourceStream("TravelApp.JsonFile.CountryCodeJson.txt");
                using (var reader = new System.IO.StreamReader(stream))
                {
                    text = reader.ReadToEnd();
                }
                return text;
            }
            catch (Exception ex)
            {
                string x = ex.Message;
                return "";
            }
        } 
    

    Best of luck

  • Ashish_sharmaAshish_sharma INMember ✭✭✭
    edited September 2019

    ">

                    <ListView x:Name="CountryCode_List" ItemsSource="{Binding CountryCodeList}" HasUnevenRows="true" 
                       CachingStrategy="RecycleElement" ItemSelected="CountryCode_List_ItemSelected">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Padding="0,0,0,10">
                                            <StackLayout Spacing="10"  Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                                <Label Text="{Binding code}" />
                                                <Label Text="{Binding name}" HorizontalOptions="StartAndExpand"/>
                                            </StackLayout>
                                     </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
    

    Design it

  • KevinhkKevinhk Member ✭✭

    this file has code of all the country when use this link countryflags
    you can show the flag as well

  • coffecoffe Member ✭✭

Sign In or Register to comment.