sanjar14
sanjar14

Reputation: 57

Using Windows 10 APIs (UWP) in Desktop app (WPF)

I have tried as described in this post (https://blogs.windows.com/buildingapps/2017/01/25/calling-windows-10-apis-desktop-application/#PdHk3f4QeTSsSvWy.97) to determine the position with the class Geolocator. It works fine when I add a reference to C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd instead of C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd. But I get an exception when I use the method GetFileAsync in the following code

    static async void LoadVoiceCommands()
    {
        try
        {
            StorageFile storageFile = await Package.Current.InstalledLocation.GetFileAsync("CustomVoiceCommandDefinitions.xml");
            await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(storageFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The exception says The process has no package identity. (Exception from HRESULT: 0x80073D54)

I call the method LoadVoiceCommands in MainWindow constructor after InitializeComponent

When I add reference C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd instead of C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd and use namespaces like:

using Windows.ApplicationModel; using Windows.Storage; using Windows.Devices.Geolocation;

there is than an error CS0731 The type forwarder for type in assembly 'Windows' causes a cycle

for the line

await locator.GetGeopositionAsync();

and

await Package.Current.InstalledLocation.GetFileAsync("CustomVoiceCommandDefinitions.xml");

Any ideas on how to solve this? I could not find a solution in google

Upvotes: 1

Views: 1705

Answers (1)

mm8
mm8

Reputation: 169200

Some WinRT API:s requires your app to run in the context of an app container.

This means that you will have to convert your app to be able to use these. You can do this by creating a Windows app package for your WPF desktop application using the Desktop Bridge.

The package gives your app an identity and with that identity it then has access to specific WinRT APIs.

Upvotes: 1

Related Questions