2013-11-25 61 views
1

我的代碼:Windows窗體Aplication文件加載異常

Listener.cs:

namespace ListenerNameSpace 
{ 

    class Listener 
    { 
     Device [] gamepad; 

     public Listener() {  } 

     public void initializeGamePad() 
     { 
      int i = 0; 
      gamepad = new Device[10];//max 10 gamepads possible due to limited USB ports 

      foreach (DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) 
      { 
       gamepad[i++] = new Device(di.InstanceGuid);//save all gamepads conected 
      } 

      if (i == 0)//no gamepads detected 
       MessageBox.Show("No gamepad detected, please connect gamepad!"); 
      else 
      { 
       //do something is a gamepad is detected 
      } 
     } 
    } 
} 

的Program.cs:

using ListenerNameSpace; 

namespace Windows_8_gamepad_UI 
{ 
    static class Program 
    {   
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
      new Listener().initializeGamePad();//I get the exception here 
     }   
    } 
} 

異常詳細信息:

混合模式組件構建針對運行時版本「v1.1.4322」,不能在沒有附加配置信息的情況下加載到4.0運行時灰。

那麼我怎麼能得到這個代碼的工作?

回答

1

Msdn 爲,

指定是否啓用在.NET Framework 2.0運行時激活 策略或使用.NET框架4激活策略。

App.confiig中,只需檢查是否爲真。 它應該是True

<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
</configuration>