Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 376
James Ko Avatar asked Feb 08 '16 19:02

James Ko


People also ask

How does .NET Core runtime work?

NET Core runs on Windows, macOS, and Linux operating systems. There are different runtime for each operating system that executes the code and generates the same output. Consistent across Architectures: Execute the code with the same behavior in different instruction set architectures, including x64, x86, and ARM.

When should you use .NET Core class library?

Use a . NET Core library when you want to increase the . NET API surface area your library can access, and you are okay with allowing only . NET Core applications to be compatible with your library.

Can we use NET Framework class library in .NET Core?

If you want to use full . NET Framework libraries, you can only target . NET Framework, unless you use multi-targeting and provide alternate implementations of those libraries for the . Net Core targets.


2 Answers

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:

  • Target.Windows
  • Target.WindowsPhone
  • Target.WindowsRuntime

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 : Application
{
    public MyApp()
    {
        var button = new Button();
        button.Content = "Hello, world!";
    }
}

and it'll just work.

like image 118
James Ko Avatar answered Sep 21 '22 00:09

James Ko


With .NET Core 3 and up (now in preview) there is a package you can install that includes most WinRT classes Microsoft.Windows.SDK.Contracts

like image 35
Mendy Avatar answered Sep 20 '22 00:09

Mendy