2017-03-29 37 views
1

嗨,我有一個由C#Windows窗體編程和偉大工程。我提出這個應用程序的所有代碼WPF項目,但在WPF的功能有一個錯誤轉換形式應用到WPF應用程序發出錯誤

的應用我宣佈這個功能,你看到CA:

[System.Runtime.InteropServices.DllImport("MNPR.dll")] 
unsafe private static extern int LP_Initialize(char* security_code, byte log_level, IntPtr hwndMsg); 

並稱之爲這裏

public MainForm() 
     { 
      InitializeComponent(); 
      unsafe 
      { 
       string security_code = "TSF"; 
       char[] security_code_p = new char[40]; 
       for (int i = 0; i < security_code.Length; i++) 
        security_code_p[i] = security_code[i]; 
       fixed (char* p = &security_code_p[0]) 
       { 
        LP_Initialize(p, 1, Handle); 
       } 
      } 

      LP_SetParams(10, 7, 20, 15, 1100, show_car_image ? (byte)2 : (byte)1, 0); 

     } 

我將所有這些代碼的WPF項目,你可以看到:

public MainViewModel() 
     { 

      string publicKey = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<RSAParameters xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <Exponent>AQAB</Exponent>\r\n <Modulus>x2U1misu3xXuLCAf79dVzUc8b1XcRJUL24gSSc41i2sY5c7nI7s5XUceuMU0o7YwHaLEp2upRI9B8PWAQzIeumFGxnui4sdJBTcY9loP7/+N85PL5MWuxBn6e3ejjwTQUEZ0QS7CgJk8jyF4Ae2M/bpWED6ZHmNs2PgYnHNQEqv3Zb5kuof4JL1C+rK9qnT5HNreF/N322uE7HbazZIPGqCnO4bO4RQupd15ztV8O4S7g5dtSucG7AxYbRl+RpxXZA1GQy2dA29wA8kaFtFoe0D4DCF+Ohfg+ySMdgZSw2BRqkOQc+r5I4ETo4KA+/9Yrm4IdHk++KxWQ5CwapRgIQ==</Modulus>\r\n</RSAParameters>"; 
      secureString = Utility.ConvertToSecureString(publicKey); 
      publicKey = string.Empty; 
      timerCallWS = new System.Timers.Timer(30000); 
      this.timerCallWS.Elapsed += new ElapsedEventHandler(this.timerCallWS_Tick); 

      NewCarReceiption = new MainModel(); 
      SaveCommand = new MCR.Commands.RelayCommand(Save, CanSave); 
      ClearCommand = new MCR.Commands.RelayCommand(Clear); 
      CaptureCommand = new MCR.Commands.RelayCommand(Capture); 
      PreviewCommand = new Commands.RelayCommand(Preview); 
      WindowLoadEvent = new DelegateCommand<object>(Load); 
      CancelAsyncCommand = new MCR.Commands.RelayCommand(CancelAsync); 
      IncorrectRecognitionCommand = new MCR.Commands.RelayCommand(IncorrectRecognition); 
      CorrectRecognitionCommand = new MCR.Commands.RelayCommand(CorrectRecognition); 
      cmbVehicleTypeChangedEvent = new DelegateCommand<object>(VehicleTypeChangedEvent); 
      cmbVehicleSystemChangedEvent = new DelegateCommand<object>(VehicleSystemChangedEvent); 
      controlGotFocusEvent = new DelegateCommand<object>(ControlGotFocusEvent); 

      this.PLQRecognitionBtnVisibilityStatus = false; 
      CancelAsyncVisibility = false; 
      MainWrapPanel = false; 


      unsafe 
      { 
       //MessageBox.Show("HID = " + LP_GetHID().ToString()); 
       string security_code = "TSF"; 
       char[] security_code_p = new char[40]; 
       for (int i = 0; i < security_code.Length; i++) 
        security_code_p[i] = security_code[i]; 
       fixed (char* p = &security_code_p[0]) 
       { 
        LP_Initialize(p, 1,Handle); 
       } 
      } 
      //it is not required to call LP_SetParams with default params, but if you want to change them, you must call it 
      LP_SetParams(10, 7, 20, 15, 1100, show_car_image ? (byte)2 : (byte)1, 0); 
      string info = "آستانه‏ی تشخیص ورود خودرو\n" + 
    "برای تصاویر شب، مقدار 7 و برای روز مقدار 20 مناسب است\n" + 
"مقدار بزرگتر حساسیت کمتری دارد و خودروی کمتری تشخیص می دهد"; 

     } 

enter image description here

正如你可以看到這部分LP_Initialize(p, 1,Handle);Handle我應該使用,而不是手柄WPF。什麼?

+0

Upvoted單獨的用戶名。 :) –

+0

@MichaelGunter :)))))) –

回答

1

WPF不像WinForms那樣直接訪問HWND。儘管如此,並不難。從您的Window子類中,你可以這樣做:

IntPtr handle = new WindowInteropHelper(this).Handle; 

UserControl子類中,你可以這樣做:

Window window = Window.GetWindow(this); 
if (window == null) 
{ 
    // The user control is not on a window. 
    // Handle this situation as appropriate. 
} 
IntPtr handle = new WindowInteropHelper(window).Handle; 
+0

我加了Window window = Window.GetWindow(this); if(window == null) { //用戶控件不在窗口中。 //根據情況處理這種情況。 } IntPtr handle = new WindowInteropHelper(window).Handle;前LP_Initialize線,但我得到了「this」關鍵字的錯誤:嚴重\t代碼\t說明\t項目\t文件\t線\t抑制狀態 錯誤\t CS1503 \t參數1:無法從「MCR.ViewModel.MainViewModel」到「系統轉換。 Windows.DependencyObject' –

+0

您的視圖模型既不是'Window'子類,也不是'UserControl'子類。如果您需要從您的視圖模型中執行'LP_Initialize',您需要開發一種方法從視圖到視圖模型獲取句柄(通過上面的代碼返回)。 –

+0

我添加了一個函數到我的視圖,你可以看到公共IntPtr GetIntPtr() { Window window = Window.GetWindow(this); if(window == null) { //用戶控件不在窗口中。 //根據情況處理這種情況。 } IntPtr handle = new WindowInteropHelper(window).Handle; return handle; },但我怎麼能在viewmodel中調用這個函數? –