none
How to use Windows.Devices.Geolocation API in a C# WinForm Win32 Desktop application in Windows 10? RRS feed

  • Question

  • Hi,

    In the MSDN documents, it indicates that to access location info from a desktop application in Windows 10, it should use Windows.Device.Geolocation API. It also says starting in Windows 10 Insider Preview, call the RequestAccessAsyn() function before accessing the user’s location.

    However, after creating a C# WinForm Win32 Desktop application project in VS 2015 RC in Windows 10, we couldn't find a way to target this app to Windows 10 (use Windows 10 SDK) and couldn't add reference to "Windows.winmd" metadata. Thus, can't use Windows.Device.Geolocation class and RequestAccessAsyn() function.

    Is it possible for C# WinForm win32 app to use this Windows 10 version of Windows.Device.Geolocation API?

    If not, how could C# WinForm win32 app or classic MFC win32 app access user location info in Windows 10?

    Thank You!

    Friday, June 26, 2015 4:19 PM

Answers

  • Hi Gary.F,

    I can repro the issue, I finally solve the problem by remove the reference to "Windows.Devices" and add reference to "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd".

    Adding a reference to "Windows.Devices" directly is a common way in Win8.0 and Win8.1, however looks like Win10 changed something so that the traditional way cannot work as expected.

    Now I can make it work under build 10074 with Visual Studio 2015RC, hope it helps.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 30, 2015 3:17 AM

All replies

  • Hi Gary.F,

    I think the class you ref is this one: RequestAccessAsync | requestAccessAsync method, which is a Windows Universal App API and if you would like to use it, please follow my steps.

    #1, To make sure if the API can be used in desktop app, base on the requirement section, Geolocator class is available with desktop app.

    #2, Unload the project by right click the project on the solution explorer.

    #3, modify the csproj file by adding the bold line into the PropertyGroup tag.

      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{8C560523-C0D0-42C6-8FE1-3E74B5F373B3}</ProjectGuid>
        <OutputType>WinExe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>WindowsFormsApplication1</RootNamespace>
        <AssemblyName>WindowsFormsApplication1</AssemblyName>
        <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <TargetPlatformVersion>10.0</TargetPlatformVersion>
      </PropertyGroup>

    #4, Reload the project and click to add reference, you should be able to find a new option "Windows Universal" exist on the panel. Choose the Windows.Devices and write the "using Windows.Devices.Geolocation;"

    Hope helps.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 29, 2015 5:59 AM
  • Thank you James.

    I tried your steps above, but I got a compile error says:

    "Severity Code Description Project File Line
    Error CS4036 'IAsyncOperation<Geoposition>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation<Geoposition>' could be found (are you missing a using directive for 'System'?) WindowsFormsApplication1 c:\users\myuser\documents\visual studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28"

    Here's the snippet:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Windows.Devices.Geolocation;

    using Windows.Foundation; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { GetLocation(); } async private void GetLocation() { Geolocator locator = new Geolocator(); var status = Geolocator.RequestAccessAsync(); if (status != null) { Geoposition pos = await locator.GetGeopositionAsync(); } } } }

    I also tried to add reference to "System.Runtime.WindowsRuntime.dll" which in "c:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\Microsoft.NET.CoreRuntime\1.0\AppLocalFramework\Neutral\System.Runtime.WindowsRuntime.dll" and it didn't solve the problem.

    Any idea to solve this error? It seems like I'm still missing some references or I did something wrong?

    Thanks a lot!

    Monday, June 29, 2015 4:48 PM
  • Hi Gary.F,

    I can repro the issue, I finally solve the problem by remove the reference to "Windows.Devices" and add reference to "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd".

    Adding a reference to "Windows.Devices" directly is a common way in Win8.0 and Win8.1, however looks like Win10 changed something so that the traditional way cannot work as expected.

    Now I can make it work under build 10074 with Visual Studio 2015RC, hope it helps.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 30, 2015 3:17 AM
  • I am using Visual Studio 2015 community version to develop app for windows universal app (special for windows phone 10) I am trying to find my current location using below code I am also facing same error. 

    Error: "'IAsyncOperation<Geoposition>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation<Geoposition>' could be found (are you missing a using directive for 'System'?) "

    My code 

     public async void getposition()
            {
                Geolocator geolocator = new Geolocator();
                Geoposition geoposition = null;
                geolocator.DesiredAccuracyInMeters = 50;
                geoposition = await geolocator.GetGeopositionAsync();  -- THIS LINE SHOW ME COMPILER ERROR 
                myMap.Center = geoposition.Coordinate.Point;
                myMap.ZoomLevel = 15;

            }

    I am using VS 2015 on Windows 10 only. 

    I am unable to find Windows.Devices reference in my project according to your above solution.

    Please guide me.

    -Dhaval M.


    DhavaL

    Sunday, October 18, 2015 5:50 AM
  • Add name space 

    using System; 

    resolve my issue.


    DhavaL

    Sunday, October 18, 2015 6:59 AM
  • Add:

    using System;

    at the head of your code file.


    Would you know my name, if I saw you in heaven......

    Friday, November 13, 2015 4:09 AM
  • Hi Gary.F,

    i have a similar problem with Bluetooth Devices.

    Did you find a solution for that?

    Thommy

    Monday, December 7, 2015 11:43 AM
  • Me too...have you solved this issue?

    Thanks

    Sunday, April 2, 2017 4:48 PM
    1. Right click on References. Select “Add Reference…” from the context menu. On the left of the Reference Manager, choose Browse and find the following file: C:Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd. Add it to your project as a reference. Note: You will need to change the filter to “All Files”.
    1. Right click on References. Select “Add Reference…” from the context menu. On the left of the Reference Manager, go to Browse and find the directory “C:Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5”. Add System.Runtime.WindowsRuntime.dll to your project.
    Friday, August 16, 2019 1:07 PM