从 Winform 应用程序获取 UWP 包信息时出错?

Error when getting a UWP package info from a Winform App?

使用 WinForm 应用程序,我试图从 Microsoft 的 Github 站点模仿此 sample,该站点显示如何使用 Windows 运行时获取程序包信息包装 API.

当我尝试从 WinForm 应用程序 获取 UWP 包信息时,我在以下代码的第 Package package = Package.Current; 行收到以下错误:

The type or namespace name 'Package' could not be found (are you missing a using directive or an assembly reference?)

问题: 虽然这个错误是网上有很多的著名C#错误posts/solutions,但是这里上下文不同。编译器似乎在抱怨我缺少 Package class 所需的程序集。但是我在下面的代码中确实有 using Windows.ApplicationModel; using 语句。那么错误的可能原因是什么?即我可能在这里遗漏了什么?

注意:为了确保包含所需的程序集,我确实在 VS2017-ver 15.9.5 [=16] 的 WinForm 项目中安装了这个 UWPDesktop NuGet 包=]:

WinForm Appthrows error行的相关代码:Package package = Package.Current;

using System;
using System.Windows.Forms;
using Windows.ApplicationModel; //I added from here
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.UI.Xaml;
using Windows.Management.Deployment;

namespace WinForms_to_UWP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Package package = Package.Current;
            PackageId packageId = package.Id;

            Console.WriteLine(packageId.FullName);
        }
    }
}

来自 GitHub 的 UWP 示例项目:来自 scenario1_identity.xaml.cs 的相关代码工作正常:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SDKTemplate;
using System;
using Windows.ApplicationModel;

namespace SDKTemplate
{
void GetPackage_Click(Object sender, RoutedEventArgs e)
{
    Package package = Package.Current;
    PackageId packageId = package.Id;

    String output = String.Format("Name: \"{0}\"\n" + packageId.FullName);

    OutputTextBlock.Text = output;
}

更新:

还值得注意的是,在顶部添加 Using Windows..... 语句时,VS intellisense 仅识别 Window.FoundationWindow.UI. 语句。对于其他 Using Windows..... 语句,我必须硬编码 - 例如,Windows.ApplicationModel;。然而,当我对它们进行硬编码时,VS2017 并没有抱怨。此外,所有以 Windows. 开头的 Using 语句都显示为灰色,如下图所示。不确定是否与错误有关:

恐怕您引用的 Nuget 包已过时,可能不再维护。

但是这个问题很容易解决。只需添加对目标 SDK 版本的 windows.winmd 文件的引用。请参阅此屏幕截图: