Reputation: 23868
Using a WinForm
App, I'm trying to mimic this sample from Microsoft's Github site that shows how to get package info by using the Windows Runtime packaging API.
I'm getting following error at line: Package package = Package.Current;
, of the code below, when trying to get a UWP package info from a WinForm app:
The type or namespace name 'Package' could not be found (are you missing a using directive or an assembly reference?)
Question: Although the error is a famous C# error that has many online posts/solutions, but here the context is different. Compiler seems to be complaining that I'm missing required assembly for Package class. But I do have using Windows.ApplicationModel;
using statement in my code below. So what may be a possible cause of the error; i.e. what I may be missing here?
NOTE: To ensure the inclusion of the required assemblies, I did install this UWPDesktop NuGet package in WinForm Project on VS2017-ver 15.9.5
on Windows 10 Pro - Ver 8109
:
WinForm App: Relevant code that throws error
at line: 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);
}
}
}
From the GitHub's UWP Sample project: The relevant Code from scenario1_identity.xaml.cs that WORKS fine:
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;
}
UPDATE:
Also worth noticing that when adding Using Windows.....
statements on the top, the VS intellisense
recognized only Window.Foundation
and Window.UI.
statements. For other Using Windows.....
statements I had to hardcode - for example, Windows.ApplicationModel;
. However VS2017
did not complain when I hard coded them. Moreover, all Using
statements starting with Windows.
are grayed out as shown in image below. Not sure if it has anything to do with the error:
Upvotes: 0
Views: 773
Reputation: 13850
The Nuget package you are referencing is outdated I am afraid and may not be maintained anymore.
But the problem is easy to fix. Just add a reference to the windows.winmd file of the SDK version you are targeting. See this screenshot:
Upvotes: 2