0

I am new to c# and visual studio and have run into some trouble.

I have created a project with references to "Windows" and ".Net" in visual studio because I want to test a little with smart cards. The code:

using System;
using Windows.Devices.Enumeration;
using Windows.Devices.SmartCards;

class HandleSmartCard 
{
    public async void checkNumberOfSmartCards()
    {
        string selector = SmartCardReader.GetDeviceSelector();
        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
        //  return "2";
    }
}

So far it looks fine. However I also want the project to be able to use System.Windows.Forms; which I have used in a previos test. I add reference to System.Windows.Forms; and try to create a form. However in that form when I try this:

Form prompt = new Form();
System.Windows.Forms.Button confirmation = new System.Windows.Forms.Button() { Dock = DockStyle.Bottom };
confirmation.Click += (sender, e) => { prompt.Close(); };

I get a red line under "Close" with the message:

Reference to type component claims it is defined in system but could not be found.

System is referenced at top of file, but I am guessing it is the wrong type of system right? Can I somehow use "both Systems" in one project so to speak? I hope someone understands what I mean and can help me understand this.

Rode
  • 51
  • 1
  • 8

1 Answers1

1

You're most likely working on a UWP app. The API for UWP apps is a very small subset of the full .NET framework. You can find more information here

https://msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx

You're attempting to reference System.Windows.Forms which is not allowed in UWP applications.

Looks like you're trying to create a popup to ask the user something. For this, use the MessageDialog class.

https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.aspx

  • The accepted answer here contains many resources for learning UWP app development. You'd be wise to read through them. https://social.msdn.microsoft.com/Forums/en-US/ec8030c9-4ba4-4222-98a8-c95496af2fe3/uwp-ask-user-for-an-input?forum=vcgeneral –  Dec 06 '16 at 17:26
  • Hmm I am not sure. I want the user to enter texts to be handled by the application, not sure I found something like that in the messagedailog examples I found. Also using System.Windows.Forms seems to be allowed just that there is some mismatching later on regarding System when actually trying to use the Form for something. But maybe that is what you meant – Rode Dec 06 '16 at 18:32
  • @Rode "using System.Windows.Forms seems to be allowed" nope. UWPs are very limited in what they can, and cannot, reference. They definitely cannot reference System.Windows.Forms.DLL. UWPs use (this is very simplified and I'm no expert) a type of assembly forwarding to support being executed on different platforms. The error is caused by this assembly forwarding failing for desktop assemblies. You can find this error all over the place, and every one of them is someone referencing a desktop assembly from a UWP. –  Dec 06 '16 at 18:41
  • Maybe I am doing this the wrong way, if the way I created the project somehow just gives me small subsets of System and so on, could I start with my "working project" and by working I mean the one where Forms works as expected and somehow add references needed to Windows.Devices.Enumeration and Windows.Devices.SmartCards. I have tried to add the references to above in my other project but haven't figuered out how. – Rode Dec 06 '16 at 18:43
  • @Rode You might want to check out https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.contentdialog.aspx to get input from the user. Also see http://stackoverflow.com/q/34623190/1228 You seem confused how this all works. I think you might have a bigger problem. It does appear you want to create a UWP app, so you should definitely start reading here https://msdn.microsoft.com/en-us/windows/uwp/get-started/universal-application-platform-guide –  Dec 06 '16 at 18:47
  • Or to be exact if I add Windows.Devices.Enumeration and Windows.Devices.SmartCards to a project were forms work. The original code above: DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector); Gives the error Error CS4036 'IAsyncOperation' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation' could be found (are you missing a using directive for 'System'?) – Rode Dec 06 '16 at 18:50
  • Well I could be wrong but I am not sure why I want a UWP?, none of the code I have made prevously seems to work in that project. I just really want a project where I can use both Forms and: string selector = SmartCardReader.GetDeviceSelector(); DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector); at the same time. – Rode Dec 06 '16 at 18:54
  • OK thank you, think im gonna use the smart card parts in a separate dll then and imort them to the rest of the project. Hopefully this works – Rode Dec 07 '16 at 22:05