将表单应用程序转换为wpf应用程序会出错

时间:2017-03-29 16:30:35

标签: c# wpf

您好我有一个由c#windows窗口编程的应用程序并且工作得很好。我将此应用程序的所有代码移动到wpf项目但是wpf中的函数有错误

我宣布这个功能,你可以看到:

 [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中的句柄?

1 个答案:

答案 0 :(得分: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;