简体   繁体   English

如何从.NET Core库引用Windows.winmd?

[英]How to reference Windows.winmd from a .NET Core library?

I'm looking to use Windows Runtime types, like Frame or Button , from a .NET Core library. 我希望使用.NET Core库中的Windows运行时类型,例如FrameButton It seemed to be working fine when I was using a traditional PCL, targeted for Windows/Windows Phone 8.1. 当我使用针对Windows / Windows Phone 8.1的传统PCL时,它似乎运行良好。 For some reason, however, it's not compiling after I switched my project to DNX. 但是由于某种原因,在我将项目切换到DNX之后,它没有编译。

Here's my project.json : 这是我的project.json

{
    "frameworks": {
        // Profile32 == Windows + Windows Phone 8.1
        ".NETPortable,Version=v4.6,Profile=Profile32": { }
    }
}

And here is my sample code: 这是我的示例代码:

using System.Linq;
using Windows.UI.Xaml.Controls;

public class Sample
{
    public void Method()
    {
        Enumerable.Empty<Frame>();
    }
}

I was getting a compiler error on Frame in this snippet, saying that the type couldn't be found. 我在此代码段中收到有关Frame的编译器错误,说找不到类型。 So, I did a little detective work on this and hit F12 on it in my regular PCL, to look at its declaring assembly. 因此,我对此进行了一些侦探工作,并在我的常规PCL中将其击中了F12,以查看其声明程序集。

It turns out that most of the Windows Runtime types I want live in a single assembly called Windows.winmd , which is found somewhere in Program Files. 事实证明,我想要的大多数Windows运行时类型都位于一个名为Windows.winmd程序集中,该程序集可在程序文件中找到。 I'm curious to know, is there any way I can reference this assembly from my .NET Core library? 我很好奇,我可以通过.NET Core库引用此程序集吗?

Thanks! 谢谢!

(Note that I can't just use a regular PCL, since I have needs that are specific to .NET Core.) (请注意,我不能只使用常规PCL,因为我有特定于.NET Core的需求。)


meta: By the way, please don't remove the asp.net-core tag from this question, since this is related to DNX. meta:顺便说一句,请不要从这个问题中删除asp.net-core标记,因为这与DNX有关。


edit: I've just tried this: 编辑:我刚刚尝试过:

".NETPortable,Version=v4.6,Profile=Profile32": {
    "frameworkAssemblies": {
        "Windows": { "type": "build" }
    }
}

Unfortunately, this doesn't seem to be working either, although the compiler has no problem resolving assemblies like System.Linq or System.Runtime . 不幸的是,尽管编译器在解析诸如System.LinqSystem.Runtime类的程序集时都没有问题,但这似乎也不起作用。

The framework moniker (TFM) are a bit confusing. 框架标记(TFM)有点令人困惑。

The Profile 32 targets "Windows 8.1, Windows Phone 8.1" (=platform standard of 1.2). 配置文件32的目标是“ Windows 8.1,Windows Phone 8.1”(= 1.2的平台标准)。

The lower the standard, the more platforms can consume the class library, but they have the fewest features. 标准越低,平台可以使用的类库越多,但功能最少。 The higher the standard, the more features can be used, but the platforms may be limited. 标准越高,可以使用的功能越多,但是平台可能会受到限制。

So if you want to support/use a winmd in your "Class Library (package)", you need to use the net (when targeting the full .NET framework) and dotnet (when targeting the .NET core framework) monikers. 因此,如果要在“类库(程序包)”中支持/使用winmd,则需要使用net(当针对完整的.NET框架时)和dotnet(当针对.NET核心框架时)绰号。

From the .NET platform standard link above: 通过上面的.NET平台标准链接:

Exising PCL projects in VS2013 and VS2015 (excluding UWP targets), can only target up to .NET Platform Standard version 1.2. VS2013和VS2015中的现有PCL项目(​​不包括UWP目标)只能定位到.NET Platform Standard 1.2以下版本。

I'm not sure if this also applies to .winmd files. 我不确定这是否也适用于.winmd文件。 But somethings you could try is to target dotnet51 , dotnet52 or dotnet53 for .NET Core. 但是您可以尝试将.NET Core定位为dotnet51dotnet52dotnet53 You can't use .NET 4.6 ( dotnet54 and dotnet55 monikers) and still target Windows 8.1, this will only work for UWP. 您不能使用.NET 4.6( dotnet54dotnet55绰号),但仍以Windows 8.1为目标,这仅适用于UWP。 According to a post of Marc Garvell the versions are off by one (so dotnet51 equals netstandard1.0 and dotnet5.5 equals netstandard1.4 ) 根据Marc Garvell帖子,版本之间dotnet51一个(因此dotnet51等于netstandard1.0dotnet5.5等于netstandard1.4

It seems that the names of the monikers are for the upcoming ASP.NET Core 1.0 RC2 version and for current RC1 you may still have to apply the dotnet monikers. 似乎这些名称的名称适用于即将发布的ASP.NET Core 1.0 RC2版本,而对于当前的RC1,您可能仍必须应用dotnet

Of course you can target multiple frameworks (for each framework defined, one assembly will be compiled), but you'll have to use preprocessor directives to remove your winmd/win8.1 classes from your code and replace it with something that works on the newer platforms. 当然,您可以针对多个框架(对于定义的每个框架,将编译一个程序集),但是您必须使用预处理程序指令从代码中删除winmd / win8.1类,并用在较新的平台。

Remember, that for Class Library (Package) you have to use "net" and "dotnet" moniker, while for .NET Core applications and test projects you still use "dnx" and "dnxcore". 请记住,对于类库(包),您必须使用“ net”和“ dotnet”绰号,而对于.NET Core应用程序和测试项目,您仍应使用“ dnx”和“ dnxcore”。

Right now it's a mess, but it should be much better with the next RC2... I hope 现在是一团糟,但是下一个RC2应该会更好...我希望

Well, looks like I found the answer. 好吧,看来我找到了答案。

The trick was to base your project off csproj instead of xproj, which you can do by following the steps here . 诀窍是使您的项目基于csproj而不是xproj,您可以按照此处的步骤进行操作。

Once you've done that, just go to your project's properties and retarget the lib to Windows 8.1 and Windows Phone 8.1. 完成此操作后,只需转到项目的属性,然后将库重新定位到Windows 8.1和Windows Phone 8.1。 Then you'll see the little assembly icon for Windows pop up, like this: 然后,您将看到弹出用于Windows的小程序集图标,如下所示:

netcoreapp3.0库中添加对Microsoft.Windows.SDK.Contractsnetcoreapp3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM