1

I am trying to convert a UWP application to a .NET application. We are heavily using the Windows namespace available only for UWP projects.

Is there an easy way to make the code compatible with .NET? Or do we have to rework everything in the .NET way?

Here are some of the classes that we are using that are not available in .NET:

  • HostName
  • StreamSocket
  • DataWriter
  • DataReader
  • StorageFile
  • ApplicationData.Current.LocalFolder
  • FileAccessMode
  • RandomAccessStream
  • InMemoryRandomAccessStream
  • InputStreamOptions
Water
  • 1,114
  • 1
  • 15
  • 31

2 Answers2

1

Many of those types will work just fine in a desktop .NET program. You just have to add a reference to the appropriate WinMD files from the SDK.

As a general rule of thumb, anything that has or uses or shows UI will not work outside of UWP, but many other things are likely to work. You can also look to see if MSDN says it works for Desktop apps -- which StreamSocket does for example -- and / or see if it has the [dualapipartition] attribute. The absence of this doesn't imply the type won't work though (eg, structs have no such attribute and sometimes people forget to add then).

In general though, if you are starting from scratch you will get much better portability (across both Windows and non-Windows OSes) by using the .NET types. For storage in particular, you will also get better performance using .NET's System.IO vs. Windows.Storage APIs.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • We found that there are some winmd we can use but there were still some issues so we decided to start from scratch. Thanks. – Water Nov 25 '16 at 06:06
  • I'm curious; which things didn't work? I know `LocalFolder` won't work but that's pretty trivial and you could have your caller provide the path. – Peter Torr - MSFT Nov 25 '16 at 16:17
  • Actually, most of the classes (ex. HostName) were resolved when we used the winmd contracts (Windows.Foundation.UniversalApiContract and Windows.Foundation.FoundationContract). However, it was throwing exceptions for code that used await (ex. IAsyncOperatino does not contain a definition for GetAwaiter, IAsynAction does not contain a definition for AsTask). So that is why we will recode instead. – Water Nov 25 '16 at 16:25
  • There should be a way to fix that -- eg try one of the answers [here](http://stackoverflow.com/questions/10428446/storagefile-async-usage-in-a-non-metro-application) – Peter Torr - MSFT Nov 25 '16 at 19:24
  • Thanks for that, it looks interesting. Will try it out. – Water Nov 28 '16 at 05:34
1

You can use extension for Visual Studio that is called .NET Portability Analyzer to check how is portable your code

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25