C# 将表单应用程序转换为wpf应用程序时出错

C# 将表单应用程序转换为wpf应用程序时出错,c#,wpf,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); 叫这里

您好,我有一个应用程序,它是由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);

        }
public MainForm()
{
初始化组件();
不安全的
{
字符串安全性\u code=“TSF”;
char[]security_code_p=新字符[40];
for(int i=0;i
我将所有这些代码移到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" +
"مقدار بزرگتر حساسیت کمتری دارد و خودروی کمتری تشخیص می دهد";

        }
public主视图模型()
{
字符串公钥="\\r\r\n\r\r\n\r\r\n AQAB\r\n\r\n\r\n AQAB\r\n\r\r\r\r\n AQAB\r\r\n\r\n AQAB\r\n\r\n X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X W2BRQKOQC+r5I4ETo4KA+/9Yrm4IdHk++KxWQ5CwapRgIQ==\r\n”;
secureString=Utility.converttosecuring(公钥);
publicKey=string.Empty;
timerCallWS=新系统.Timers.Timer(30000);
this.timerCallWS.appeased+=新的ElapsedEventHandler(this.timerCallWS_勾选);
NewCarReceiption=新的主模型();
SaveCommand=new MCR.Commands.RelayCommand(Save,CanSave);
ClearCommand=新的MCR.Commands.RelayCommand(清除);
CaptureCommand=新的MCR.Commands.RelayCommand(捕获);
PreviewCommand=新命令。RelayCommand(预览);
WindowLoadEvent=新的DelegateCommand(加载);
CancelAsyncCommand=新的MCR.Commands.RelayCommand(CancelAsync);
IncorrectRecognitionCommand=新的MCR.Commands.RelayCommand(IncorrectRecognition);
CorrectRecognitionCommand=新的MCR.Commands.RelayCommand(CorrectRecognition);
cmbVehicleTypeChangedEvent=新的委派命令(VehicleTypeChangedEvent);
CMBCVehicleSystemChangedEvent=新的委派命令(VehicleSystemChangedEvent);
controlGotFocusEvent=新的DelegateCommand(controlGotFocusEvent);
this.PLQRecognitionBtnVisibilityStatus=假;
CancelAsyncVisibility=false;
MainWrapPanel=false;
不安全的
{
//MessageBox.Show(“HID=“+LP_GetHID().ToString());
字符串安全性\u code=“TSF”;
char[]security_code_p=新字符[40];
for(int i=0;i


正如您所看到的,这部分
LP\u Initialize(p,1,Handle);
Handle
在wpf中我应该使用什么来代替Handle?

wpf不像WinForms那样提供对HWND的直接访问。不过,获取它并不困难。在
窗口
子类中,您可以执行以下操作:

IntPtr handle = new WindowInteropHelper(this).Handle;
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;
UserControl
子类中,您可以执行以下操作:

IntPtr handle = new WindowInteropHelper(this).Handle;
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;

仅对用户名进行向上投票。:@MichaelCounter:()()()()()())()()()))我在LP u初始化行之前添加了Window Window Window Window Window Window Window=Window.GetWindow(this);if(Window==null“关键字:严重性代码说明项目文件行抑制状态错误CS1503参数1:无法从'MCR.ViewModel.MainViewModel'转换为'System.Windows.DependencyObject'您的视图模型既不是窗口子类,也不是用户控件子类。”。如果需要从视图模型执行
LP_Initialize
,则需要开发一种方法,将句柄(通过上面的代码返回)从视图获取到视图模型。我在视图中添加了一个函数,因为您可以看到public IntPtr GetIntPtr(){Window Window Window=Window.GetWindow(this);If(Window==null){//用户控件不在窗口上//请酌情处理此情况。}IntPtr Handle=new WindowInteropHelper(window).Handle;return Handle;},但是如何在viewmodel中调用此函数?请看一下: