Monday 1 September 2014

Find Longitude and Latitude in Windows Phone

Hi All,


Refer below code to do the same.

public async static Task<string> FindLongitudeAndLatitude()
        {
            string latitude = string.Empty;
            string longitude = string.Empty;
            Geolocator oGeolocator = new Geolocator();
            try
            {
                //Geoposition geoposition = await     oGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(10));
                Geoposition geoposition = await oGeolocator.GetGeopositionAsync();
                latitude = geoposition.Coordinate.Latitude.ToString("0.00");
                longitude = geoposition.Coordinate.Longitude.ToString("0.00");
            }
            catch (Exception) { }
            return latitude + " & " + longitude;
        }

How to get IP Address in Windows Phone

Hi All,

For generating IP Address for your device,use this below code.

private static IPAddress GetIPAddress()
        {
            List<string> IpAddress = new List<string>();
            var Hosts =Windows.Networking.Connectivity.NetworkInformation.GetHostNames().ToList();
            foreach (var Host in Hosts)
            {
                string IP = Host.DisplayName;
                IpAddress.Add(IP);
            }
            IPAddress address = IPAddress.Parse(IpAddress.Last());
            return address;
        }

Http Server For Windows Phone

Hi All,


For acting windows phone as server,use this below link for code and documentation.

Find whole code for download at this link


Enjoy...


Happy Coding :)

Call awaited async() from sync() method

Hi All,

For calling Task awaited async() methods from any normal function we need to make current thread awaited,instead of asynchronously running an independent method.

public void LoadFileAsync()
{

 LoadFileFromStorage().wait();

}

async Task LoadFileFromStorage()
{
//Loading file started...

/*
 Do your stuff
*/

Load file Completed

}



Friday 4 July 2014

How to get Device Free Space(Memory) in Windows Phone

Hi All,

Use this below Function to do the same.

public static long GetDeviceRemainingSpace()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return store.AvailableFreeSpace;
            }
        }

Thursday 3 July 2014

How to call API using Windows.Web.Http.HttpClient

public async Task<HttpResponseMessage> GetAsync(Uri uri, string contentType = "application/json")
        {
            Logx.Info("HttpManager.GetAsync(" + uri + ", " + contentType + ')');
            HttpClient httpClient = GetHttpClient();
            HttpResponseMessage response = null;
            HttpMediaTypeHeaderValue mthv;
            HttpStringContent content;

            httpClient.DefaultRequestHeaders.Clear();
            httpClient.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
            httpClient.DefaultRequestHeaders.Add(ServiceConstants.HEADER_USER_AGENT, "P2PWindows");
            httpClient.DefaultRequestHeaders.Add(ServiceConstants.HEADER_API_VERSION, "1.0");
            httpClient.DefaultRequestHeaders.Add(ServiceConstants.HEADER_OS, "windows");

            mthv = new HttpMediaTypeHeaderValue(contentType);
            content = new HttpStringContent(string.Empty);
            content.Headers.ContentType = mthv;
            string data = string.Empty;
            try
            {
                response = await httpClient.GetAsync(uri);
            }
            catch (Exception e)
            {
                Logx.LogException(e, "HttpManager.GetAsync(" + uri + ", " + contentType + ')');
                // Check if task was cancelled
                var tce = e as TaskCanceledException;
                if (tce != null)
                {
                    if (TaskStatus.Canceled.Equals(tce.Task.Status))
                    {
                        // Task was cancelled
                        throw tce;
                    }
                }
            }

            return response;
        }

Tuesday 1 July 2014

How to Reload/Refresh Page in Windows Phone

void ReloadPage()
{
NavigationService.Navigate(new Uri(NavigationService.Source + "?retry=true", UriKind.Relative));
}

 protected override void OnNavigatedTo(NavigationEventArgs e)
  {
     if (NavigationContext.QueryString.ContainsKey("retry"))
        {
            NavigationService.RemoveBackEntry();
         }
      base.OnNavigatedTo(e);
   }

Note:-Call ReloadPage() whenever you need to refresh/reload page.





How to set App bar color for throughtout Windows Phone App

Hi All,

Here is code to set Application properties for changing things through out the application.

Write this below lines in App.xaml file.

 <Application.Resources>
                 <Style x:Key="AppBarStyle" TargetType="phone:PhoneApplicationPage">
            <Setter Property="shell:SystemTray.BackgroundColor" Value="#067AB4" />
        </Style>
         </Application.Resources>


Where #067AB4 is the color for app bar.


Happy Coding :)



How to get PowerSourceChanged Event in Windows Phone

Here is code to add event listner and used dispatcher to launch it.

Note:-Write this code in .cs file of .xaml file,not in ordinary/view model class file.

init()
{
  DeviceStatus.PowerSourceChanged += DeviceStatus_PowerSourceChanged;
}

 void DeviceStatus_PowerSourceChanged(object sender, EventArgs e)
        {
            DeviceStatus.PowerSourceChanged -= DeviceStatus_PowerSourceChanged;
            this.Dispatcher.BeginInvoke(() =>
            {
                              //Write code here...
            }
          );
        }