简体   繁体   English

当可以从 WPF 和 WinForm 应用程序打开相同的对话框时,为对话框设置父级

[英]Set Parent for the dialog box when the same dialog box can be opened from both WPF and WinForm application

I have an application which have 2 modules (one made from WinForms and other made of WPF).我有一个应用程序,它有 2 个模块(一个由 WinForms 制成,另一个由 WPF 制成)。 I have an Export Button on both of the modules.我在两个模块上都有一个导出按钮。

Export button is used to Export the grid to Excel File and save on my local.导出按钮用于将网格导出到 Excel 文件并保存在我的本地。

I have a common method (ExportToExcel()) for exporting grid to excel when Export button is clicked.单击“导出”按钮时,我有一个通用方法(ExportToExcel())将网格导出到 excel。

Now as i have said my application has both WPF and WinForms used, now when the user clicks on Export Button on 1st module(WinForms) the Export dialog box opens, Now if the user clicks on Export Button of 2nd Module(WPF) the issue was thrown Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.现在正如我所说,我的应用程序同时使用了 WPF 和 WinForms,现在当用户单击第一个模块(WinForms)上的导出按钮时,导出对话框打开,现在如果用户单击第二个模块(WPF)的导出按钮问题在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。 Ensure that your Main function has STAThreadAttribute marked on it.确保您的 Main 函数上标记了 STAThreadAttribute。 This exception is only raised if a debugger is attached to the process.仅当调试器附加到进程时才会引发此异常。 which i have corrected applying threading as shown in below code using thread.SetApartmentState(ApartmentState.STA).我已经使用thread.SetApartmentState(ApartmentState.STA)更正了应用线程,如下面的代码所示。

Now the issue after using threading and setting ApartmentState to STA is that the parent has been removed from the Export dialog box, which should be the particular module(1st or 2nd) from where the Export button was clicked and dialog box was opened, right now the parent for the dialog box is windows not the application modules(1st or 2nd).现在使用线程并将 ApartmentState 设置为 STA 后的问题是父级已从“导出”对话框中删除,该对话框应该是单击“导出”按钮并打开对话框的特定模块(第一个或第二个),现在对话框的父级是窗口而不是应用程序模块(第一个或第二个)。

Another issue is that as the parent is not set for the Export Dialog box , if the user clicks on Export button multiple times multiple Export dialog box get opened, which is not correct, once the export button is clicked and the dialog box gets opened, user should not be able to click on export box again until and unless user completes operation on dialog box or closes it.另一个问题是,由于没有为导出对话框设置父级,如果用户多次单击导出按钮,则会打开多个导出对话框,这是不正确的,一旦单击导出按钮并打开对话框,除非用户完成对对话框的操作或将其关闭,否则用户不应再次单击导出框。

My requirement is, the parent for the dialog box should be the one module from where the dialog box is opened and user should not be able to perform any operation on the application until the dialog box operation is completed.我的要求是,对话框的父级应该是打开对话框的一个模块,并且用户在对话框操作完成之前不能对应用程序执行任何操作。

Code for Export button click导出按钮单击的代码

public void ExportToExcel(UltraGrid gridToExport, UltraGridExcelExporter ultraGridExcelExporter, string name)
{
    Thread thread = new Thread((ThreadStart)(() =>
    {
     // code to open dialog box and perform operation on it.
    }));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

I'm not sure what you said but try this:我不确定你说的是什么,但试试这个:


   //You need to enable (IsMdiContainer)
   //Example, in your WindowsForms set:
   this.IsMdiContainer = true;

   WpfMenu formMenu = new WpfMenu();
   formMenu.MdiParent = this;
   formMenu.Show();

This makes "forms Menu", be a child of your Forms that is being executed, I believe it works for WPF too这使得“表单菜单”成为正在执行的表单的子项,我相信它也适用于 WPF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM