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

时间:2019-01-28 21:59:13

标签: c# uwp desktop-bridge

我正在尝试使用WinForm应用程序来模仿Microsoft Github网站上的sample,该网站显示了如何使用Windows运行时打包API来获取打包信息。

当尝试从 WinForm应用程序获取UWP软件包信息时,以下代码的Package package = Package.Current;行出现错误:

  
    

找不到类型或名称空间名称“ Package”(您是否缺少using指令或程序集引用?)

  

问题:尽管该错误是著名的C#错误,具有许多在线帖子/解决方案,但此处的上下文有所不同。编译器似乎在抱怨我缺少Package class所需的程序集。但是我在下面的代码中确实有using Windows.ApplicationModel; using语句。因此,可能是导致错误的原因;即我在这里可能会想念什么?

注意:为了确保包含必需的程序集,我确实在VS2017-ver 15.9.5Windows 10 Pro - Ver 8109的WinForm Project中安装了此UWPDesktop NuGet软件包:

WinForm应用程序throws errorPackage 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在我对它们进行硬编码时并没有抱怨。此外,以Using开头的所有Windows.语句都是灰色的,如下图所示。不确定是否与错误有关:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

但是问题很容易解决。只需添加对您要定位的SDK版本的windows.winmd文件的引用即可。查看此屏幕截图:

enter image description here

相关问题