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

发布于 2025-02-07 09:08:03 字数 998 浏览 1 评论 0原文

我有一个具有2个模块的应用程序(一个由Winforms制成,另一个由WPF制成)。我在两个模块上都有一个导出按钮。

导出按钮用于导出电网以excel文件并保存在我的本地上。

我有一个通用方法(ExportToExcel())用于单击导出按钮时将电网导出到Excel。

现在,正如我已经说过的,我的应用程序同时使用了WPF和WinForms,现在用户单击1st模块上的导出按钮(WinForms)(Winforms)“导出”对话框打开,如果用户单击2nd Module的导出按钮(WPF),则该问题在进行OLE呼叫之前,必须将当前线程设置为单线公寓(STA)模式。确保您的主要功能在其上标​​记了刻板函数。 当附加调试器附加到该过程中。

仅 使用螺纹和将公寓式设置设置为sta是从“导出”对话框中删除了父,这应该是单击“导出”按钮并打开对话框的特定模块(第一或第二),现在是父母的父母。对话框是Windows不是应用程序模块(第一或第二个)。

另一个问题是,由于未为“导出”对话框设置父,如果用户单击导出按钮多次开放多次导出对话框,这是不正确的,一旦单击导出按钮并打开对话框,则用户不应再次单击导出框,除非用户在对话框上完成操作或将其关闭。

我的要求是,对话框的父母应该是打开对话框的一个模块,并且在对话框操作完成之前,用户不应在应用程序上执行任何操作。

导出按钮的代码单击

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 have an application which have 2 modules (one made from WinForms and other made of 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.

I have a common method (ExportToExcel()) for exporting grid to excel when Export button is clicked.

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. Ensure that your Main function has STAThreadAttribute marked on it. 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).

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).

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2025-02-14 09:08:03

我不确定您在说什么,但请尝试以下操作:


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

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

这使得“表单菜单”,成为正在执行的表格的孩子,我相信它也适用于WPF

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文