如何从.NET Core库引用Windows.winmd? [英] How to reference Windows.winmd from a .NET Core library?

查看:507
本文介绍了如何从.NET Core库引用Windows.winmd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用.NET Core库中的Windows运行时类型,例如 Frame Button 。当我使用针对Windows / Windows Phone 8.1的传统PCL时,它似乎运行良好。但是由于某种原因,在我将项目切换到DNX之后,它没有编译。



这是我的 project.json

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

这是我的示例代码:

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

公共类样本
{
public void Method()
{
Enumerable.Empty< Frame>();
}
}

我在<$ c $上遇到编译器错误c> Frame 在此代码段中,表示找不到该类型。因此,我对此做了一些侦探工作,并在我的常规PCL中按了F12,以查看其声明程序集。



事实证明,大多数Windows我希望运行时类型位于一个名为 Windows.winmd 的程序集中,该程序集可在Program Files中找到。我很好奇,我可以从.NET Core库中引用此程序集吗?



谢谢!



(请注意,我不能只使用常规PCL,因为我有特定于.NET Core的需求。)






元:顺便说一句,请不要从此问题中删除 asp.net-core 标记,因为






edit:我刚刚尝试过:

p>

 。NETPortable,版本= v4.6,配置文件= Profile32:{
frameworkAssemblies:{
Windows:{ type: build}
}
}

不幸的是,尽管编译器解决诸如 System.Linq System.Runtime之类的程序集也没有问题,但这似乎也不起作用。

解决方案

好吧,看来我找到了答案。



诀窍是建立您的项目关闭csproj而不是xproj,您可以按照


I'm looking to use Windows Runtime types, like Frame or Button, from a .NET Core library. It seemed to be working fine when I was using a traditional PCL, targeted for Windows/Windows Phone 8.1. For some reason, however, it's not compiling after I switched my project to DNX.

Here's my 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. So, I did a little detective work on this and hit F12 on it in my regular PCL, to look at its declaring assembly.

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. I'm curious to know, is there any way I can reference this assembly from my .NET Core library?

Thanks!

(Note that I can't just use a regular PCL, since I have needs that are specific to .NET Core.)


meta: By the way, please don't remove the asp.net-core tag from this question, since this is related to 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.

解决方案

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.

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. Then you'll see the little assembly icon for Windows pop up, like this:

这篇关于如何从.NET Core库引用Windows.winmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆