Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF应用程序内运行exe应用程序,命令和单击之间是否存在差异?_C#_Wpf - Fatal编程技术网

C# 在WPF应用程序内运行exe应用程序,命令和单击之间是否存在差异?

C# 在WPF应用程序内运行exe应用程序,命令和单击之间是否存在差异?,c#,wpf,C#,Wpf,我试图在WPF应用程序中运行一个应用程序,下面是搜索答案后的代码 [DllImport("user32.dll", EntryPoint = "SetParent", SetLastError = true)] private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll"

我试图在WPF应用程序中运行一个应用程序,下面是搜索答案后的代码

    [DllImport("user32.dll", EntryPoint = "SetParent", SetLastError = true)]
    private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", EntryPoint = "ShowWindow")]
    private static extern long ShowWindow(IntPtr hWnd, int nCmdShow);

       public void ShowWindow(){
       System.Windows.Forms.PictureBox childp = new System.Windows.Forms.PictureBox()
        {
            Height = 1000,
            Width = 1000
        };
        winform.Child = childp;


         ProcessStartInfo psi = new ProcessStartInfo("C:\\Windows\\system32\\mspaint.exe")
         {
              WindowStyle = ProcessWindowStyle.Minimized
          };

         Process PR = Process.Start(psi);

        // true if the associated process has reached an idle state:
        PR.WaitForInputIdle();
       

        // loading exe to the wpf window:
        SetParent(PR.MainWindowHandle, childp.Handle);
        ShowWindow(PR.MainWindowHandle, 3);
        }
xaml代码是:

       <TabControl>
            <TabItem Header="localhost">
                <StackPanel>
                    <wfi:WindowsFormsHost  x:Name="winform"/>
                    <Button Command = "{Binding ShowWindowCommand}">Show Window<Button/>
                </StackPanel>
            </TabItem>
            <TabItem Header="File1">
            </TabItem>
        </TabControl>

橱窗

但是此代码没有正确运行,mspaint.exe从WPF应用程序中运行,函数
ShowWindow()
由命令
ShowWindowCommand
调用,并且没有用处,但是如果由
调用,请单击
并更改
PR.WaitForInputIdle()
线程。sleep
,它可以工作,但为什么?

命令和单击之间的区别是:

  • 用于在ViewModel类中执行ICommand属性的命令
  • 单击using执行代码隐藏类中定义的方法
如果要使用该命令,需要创建ICommand属性并在那里执行代码。以下是一个例子:

        private ICommand _showPaintCommand;

        public ICommand ShowPaintCommand
        {
            get
            {
                if (_showPaintCommand == null)
                {
                    _showPaintCommand = new RelayCommand(
                        param => ShowPaint(),
                        param => true);
                }

                return _showPaintCommand;
            }
        }

        private void ShowPaint()
        {
            ProcessStartInfo psi = new ProcessStartInfo("C:\\Windows\\system32\\mspaint.exe")
            {
                WindowStyle = ProcessWindowStyle.Minimized
            };

            Process PR = Process.Start(psi);
        }
如何在此处创建RelyCommand类:

命令和单击的不同之处在于:

  • 用于在ViewModel类中执行ICommand属性的命令
  • 单击using执行代码隐藏类中定义的方法
如果要使用该命令,需要创建ICommand属性并在那里执行代码。以下是一个例子:

        private ICommand _showPaintCommand;

        public ICommand ShowPaintCommand
        {
            get
            {
                if (_showPaintCommand == null)
                {
                    _showPaintCommand = new RelayCommand(
                        param => ShowPaint(),
                        param => true);
                }

                return _showPaintCommand;
            }
        }

        private void ShowPaint()
        {
            ProcessStartInfo psi = new ProcessStartInfo("C:\\Windows\\system32\\mspaint.exe")
            {
                WindowStyle = ProcessWindowStyle.Minimized
            };

            Process PR = Process.Start(psi);
        }
如何在此处创建RelyCommand类: