0

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

sanjar14
  • 47
  • 1
  • 10
  • https://stackoverflow.com/questions/31930452/are-cortana-apis-available-for-desktop-applications – Hans Passant May 20 '18 at 14:51
  • @HansPassant Of course is Windows.Media.SpeechRecognition available in classic apps. I tested it myself. It works for Windows 10, but not for Windows 8 – sanjar14 May 20 '18 at 16:23

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • My goal is to use the APIs of Windows 10 in a classic app. I do not need a Windows app. In this case I want to use the speech recognition of Windows.Media in WPF. I can already use the dictation function in my WPF application. However, I also want to use the Voice Command Defintion (vcd) as it is commonly used for cortana in my WPF application. – sanjar14 May 21 '18 at 13:50
  • The Microsoft docs page says that Package.Current is only supported in UWP and returns the package for a Windows app. This is the cause of the exception in my code. For the Classic App, use Windows.Management.Deployment.PackageManager. However, I do not know how I hereby can load my xml file with the voice commands with that – sanjar14 May 21 '18 at 13:52
  • Your question reads "I have tried..." and "the exception says". Your latest comment says something completely different. – mm8 May 21 '18 at 13:56
  • My goal is to use the speech recognition of Windows.Media in WPF. For this I tried to read the predefined voice commands from the xml file .According to research today I found out that I can not use Package.Current in an classic app. Now I'm looking for another way to solve this. – sanjar14 May 21 '18 at 14:07
  • The soultion would be to use the desktop bridge. Then you can use Package.Current. – mm8 May 21 '18 at 14:10
  • The desktop bridge converts the wpf application into a Windows app, but not vice versa. I do not need a windows app. I have to integrate the speech recognition function later in a larger, existing WPF application. – sanjar14 May 21 '18 at 14:17
  • "Windows app"? WPF is Windows only. And you do obviously do need an app package which is why I suggest using the desktop bridge. What do you mena by "not vice versa"? You need to give your WPF application an app identify to be able to call this specific WinRT API. – mm8 May 21 '18 at 14:21
  • With windows app I mean the UWP app. The desktop bridge converts your wpf app in an UWP app. – sanjar14 May 21 '18 at 14:31
  • Yes. A non-pure UWP app. Or a WPF app with a package identify. Which is what you want. – mm8 May 21 '18 at 14:39
  • Do you have any links or videos on how to do this? As far as I know, I can't give a wpf application an package identity with the desktop bridge and afterwards using it as a wpf application. – sanjar14 May 21 '18 at 15:01
  • I supplied a link in my answer. How would you be able to use if not as a WPF application...? – mm8 May 21 '18 at 15:02
  • Of course, after converting with the desktop bridge, it will still be a wpf application. But when I use it, it's not a classic wpf application, but a UWP app. And that's what I do not want. I do not want to integrate my WPF application into a UWP app. – sanjar14 May 21 '18 at 15:17
  • So what’s your definition of a ”wpf application” versus a ”classical wpf application”? Obviously you cannot use this API in a ”classical” one that doesn’t run inside a container. – mm8 May 21 '18 at 15:35