James Ko February 2016

How to use Windows Runtime classes in .NET Core libraries?

I'm developing a library for use with WPF and Windows 10. I'm running into issues getting it to compile on the latter. Here is some of the code:

project.json

{
    "frameworks": {
        "net46": {
            "frameworkAssemblies": {
                "WindowsBase": "4.0.0.0"
            }
        },
        "netcore50": {
            "dependencies": {
                "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
            }
        }
    }
}

Dependency.cs

using System;
using System.Collections.Generic;
#if NET46
using System.Windows; // .NET Framework 4.6
#elif NETCORE50
using Windows.UI.Xaml; // Windows 10 apps
#endif

public static class Dependency
{
    public static DependencyProperty Register<T, TOwner>(string name, PropertyChangedCallback<T, TOwner> callback)
        where TOwner : DependencyObject
    {
        // Code here....
    }
}

While this compiles fine for net46 (which is the traditional .NET Framework), I'm having trouble getting it to work for netcore50 (which can be used by Windows 10 apps). For some reason, it looks like types like DependencyProperty or DependencyObject are not included in that configuration.

Is there a netcore50-compatible NuGet package I can install that contains these types, so I can use them from my library?

Thanks for helping.


EDIT: I just typed in DependencyProperty in VS and hit F12. It appears that the type lives in the Windows.Foundation.UniversalApiContract assembly, but there's no such package on NuGet.

Answers


Chris McGrath February 2016

WPF Isn't compatible with .net Core nor are W10 Universal Apps, to my knowledge only Console apps and web apps are currently compatible with .net core, you should be able to still use the the new code base with the new project system but you will need to remove .net core from your configuration in order to compile if you want to use .net core with linux with a desktop app you will simply have to wait, or use a compatible windowed app framework ( if any are available yet), you should be able to use a cross platform framework base around html/js such as Electron or Cordova ( not sure on this one on whether there is a desktop app framework with Cordova)


James Ko February 2016

Finally solved the problem on my own! (If you're looking for a quick answer, you may want to scroll down.)

I remembered by chance that the .NET Core GitHub repo had a bunch of WinRT-specific libraries, like System.Runtime.WindowsRuntime. So, I headed over there to see how they did it.

It appears they use some kind of internally-hosted "targeting pack", which contains a single Windows.winmd file (which holds all the types in the Windows Runtime), to achieve this affect. Unfortunately, the package is hosted on a private NuGet feed meant only for the .NET Core team, so I can't use it.

I've opened an issue about this on the CoreFX repo here, so I can petition Microsoft for an official solution to this problem. In the meantime, I've taken matters into my own hands. I've found all the different versions of Windows.winmd on my laptop, and uploaded them as NuGet packages. Here they are:

You can use them like this:

"frameworks": {
    ".NETPortable,Version=v4.5,Profile=Profile32": {
        "dependencies": {
            "Target.WindowsRuntime": "8.1.2"
        }
    }
}

After that, you'll be able to write something like this:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

public class MyApp : Applicat 

Post Status

Asked in February 2016
Viewed 3,018 times
Voted 5
Answered 2 times

Search




Leave an answer