【问题标题】:Run exe application inside WPF application, Different between Command and Click?在WPF应用程序中运行exe应用程序,Command和Click之间的区别?
【发布时间】:2020-07-22 17:54:50
【问题描述】:

我正在尝试在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调用并无用,但如果被Click调用并更改@987654326 @ to Thread.sleep ,它有效,但是为什么呢?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    Command和Click的区别是:

    • 用于在 ViewModel 类中执行 ICommand 属性的命令。
    • 单击使用以执行代码隐藏类中定义的方法。

    如果你想使用命令,你需要创建一个 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 类:
    https://www.c-sharpcorner.com/UploadFile/20c06b/icommand-and-relaycommand-in-wpf/

    【讨论】:

      猜你喜欢
      • 2012-01-19
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 2011-04-03
      相关资源
      最近更新 更多