Tuesday, December 23, 2014

MasterDetailPage example in xamarin forms

Q: How to use Master Detail Page in Xamarin Forms.
Ans:

 1. Create a Mobile Project using "Visual Studio" or "Xamarin Studio".
 
 
 2. Create a page in shared section and type the code below.
               public class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            var menuPage = new MenuPage();
            menuPage.Menu.ItemSelected += (sender, e) => NavigateToPage(e.SelectedItem as                             MenuItem);
            Master = menuPage;
            Detail = new NavigationPage(new HomePage());
        }
        void NavigateToPage(MenuItem menu)
        {
            Page displayPage = (Page)Activator.CreateInstance(menu.TargetType);

            Detail = new NavigationPage(displayPage);

            IsPresented = false;
        }
    }

 3. Create a MenuPage like below
                public class MenuPage : ContentPage
               {
                         public ListView Menu { get; set; }

                         public MenuPage()
                         {
                                        Icon = "settings.png";
                                        Title = "Menu"; // The Title property must be set.
                                        BackgroundColor = Color.FromHex("333333");
                                        Menu = new MenuListView();
                                        var layout = new StackLayout
                                       {
                                               Spacing = 0,
                                               VerticalOptions = LayoutOptions.FillAndExpand
                                        };
                                        layout.Children.Add(Menu);
                                        Content = layout;
                            }
                 }

 4. Create the "ManuItem" class, like below

               public class MenuItem  {
                        public string Title { get; set; }
                        public string IconSource { get; set; }
                        public Type TargetType { get; set; }
               }

 5. Create a class inheritance listview, like below

    public class MenuListView : ListView
    {
        public MenuListView()
        {
            List<MenuItem> data = new MenuListData();

            ItemsSource = data;
            VerticalOptions = LayoutOptions.FillAndExpand;
            BackgroundColor = Color.Transparent;

            var cell = new DataTemplate(typeof(ImageCell));
            cell.SetBinding(TextCell.TextProperty, "Title");
            cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");

            ItemTemplate = cell;
            //SelectedItem = data[0];
        }
    }

public class MenuListData : List<MenuItem>
    {
        public MenuListData()
        {
            this.Add(new MenuItem()
            {
                Title = "Home",
                TargetType = typeof(HomePage)
            });

            this.Add(new MenuItem()
            {
                Title = "About Us",
                TargetType = typeof(AboutUsPage)
            });
        }
    }


 6. Create the content pages according to your requirement.
   public class HomePage : ContentPage
    {
        public HomePage()
        {
            BackgroundColor = Color.Yellow;
            Title = "Home Page";
            Content = new Label
            {
                Text = "Home Page!",
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
        }
    }

    public class AboutUsPage : ContentPage
    {
        public AboutUsPage()
        {
            BackgroundColor = Color.Green;
            Title = "AboutUs Page";
            Content = new Label
            {
                Text = "About Us Page!",
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
        }
    }

 7. Output



Monday, December 22, 2014

Add PinPoint To Xamarin Forms Maps

1. How to add PinPoint to Xamarin Forms Maps.

Ans: 1. Install Xamarin.Forms.Maps package from "NuGet Package".
        2. Use the below code

Map map;
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(latitude,longitude), Distance.FromMiles(App._mapZoom)));
map.Pins.Add(new Pin
                {
                    Label = addressQuery,
                    Position = position,
                    Address = addressQuery
                });



Read DeviceId in Xamarin Android

Q : How to read Device-id in Xamarin Android?

Ans:

Some times we want to read Device-id while developing the app's.
this code will help you to read the device-id in Xamarin Android.
i use the below code to read the device-id in my app.
  1. Create a Xamarin Android project.
  2. Right click on your project -> properties -> Android Mainfest.
  3. it will display as below.
  4. select the READ_PHONE_STATE option.
  5. then go to your activity page.
  6. add the line"using Android.Telephony;".
  7. you can write the code in OnCreate method.
         TelephonyManager tM = (TelephonyManager)GetSystemService(Context.TelephonyService);
         String uid = tM.DeviceId;

you can read the device-id like this.