ApplicationData using in Console with UWP parameters
Asked Answered
M

3

5

I'm trying to follow UWP with Desktop Extension – Part 2 of UWP and WinForms desktop-bridge calling the processes and passing parameters. This example Console Program.cs code includes parameters string:

 string parameters = ApplicationData.Current.LocalSettings.Values["parameters"] as string;

But the name ApplicationData does not exists in the current context, I'm trying to find out, if I've missed some reference or it is different version of C#

I'm not sure even if it is what it requires, but adding of reference Windows.Foundation.UniversalApiContract.windmd throws another error with Values:

Error CS0012 The type 'IPropertySet' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows.Foundation.FoundationContract

Monadnock answered 4/7, 2019 at 2:28 Comment(0)
S
4

If you want to use ApplicationData class, please add Windows.winmd where in the C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd. Then add Windows.Storage namespace.

Detail steps

Right click project References -> Add References -> Browse(file type all file)-> select Windows.winmd

Shorten answered 4/7, 2019 at 7:53 Comment(1)
Apparently this does not work with .NET 5.0: error NETSDK1130: Windows.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130Hematite
W
6

@Nico Zhu - MSFT is correct, but I would also suggest alternative approach, which may make your life easier in the long run - the UWP APIs for desktop apps are now also distributed via NuGet as a package which takes care of referencing the right libraries for you.

It is in preview at the time of writing, so you can install as follows:

Install-Package Microsoft.Windows.SDK.Contracts -Version 10.0.18362.2002-preview

See the NuGet page for more info.

Wadsworth answered 4/7, 2019 at 8:10 Comment(4)
Didn't work for me, Windows.Storage namespace was not being resolved after installing that packageCounterproductive
@Martin Zikmund, I currently have this package installed in a winforms app (packaged) but can't access ApplicationData.Current.LocalSettings.Values. Is there a capability or a manual Package.appxmanifest edit required? Also need access to LocalFolder.Aventine
@Martin Zikmund, I ended up using try-convert to get it over to .NET 5Aventine
@CarloMendoza please, could you clarify how you did it?Slier
S
4

If you want to use ApplicationData class, please add Windows.winmd where in the C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd. Then add Windows.Storage namespace.

Detail steps

Right click project References -> Add References -> Browse(file type all file)-> select Windows.winmd

Shorten answered 4/7, 2019 at 7:53 Comment(1)
Apparently this does not work with .NET 5.0: error NETSDK1130: Windows.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130Hematite
S
1

From .NET6 or later it is not working. Instead you should change the win32 project TargetFramework and specify the windows version.

For example if you use .NET8 and want to target Windows 11:

<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>

You can also set the TargetPlatformMinVersion:

<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>

If you do this and reopen the project you can see a new project reference named Microsoft.Windows.SDK.NET.Ref automatically added.

More info: https://learn.microsoft.com/hu-hu/windows/apps/desktop/modernize/desktop-to-uwp-enhance

Schauer answered 3/4 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.