【问题标题】:Check if window from loaded assembly is open检查加载程序集的窗口是否打开
【发布时间】:2018-04-25 22:41:21
【问题描述】:

我有以下代码在单击按钮以从另一个 application.exe 打开一个窗口时执行

gAssembly = Assembly.LoadFrom(JLDBConnection.Properties.Settings.Default.DefaultString + @"\JLRetailTerminal.exe");
Type typ = gAssembly.GetExportedTypes().Where(s => s.Name == "RetailWindow").FirstOrDefault();
typ.GetMethod("Show").Invoke(Activator.CreateInstance(typ), null);

gAssembly 是一个全局变量。
如何检查“RetailWindow”是否已经打开并且不打开另一个窗口?

【问题讨论】:

    标签: c# .net wpf windows .net-assembly


    【解决方案1】:

    你是唯一一个创造它的人吗?如果是这样,您可以存储窗口实例并检查 IsVisible:

    即:

    g_wnd = (Window)Activator.CreateInstance(type);
    
    if (!g_wnd.IsVisible)
    {
    }
    

    【讨论】:

    • 神奇地工作,必须扩展以检查窗口是否已关闭并且确实需要重新打开。
    【解决方案2】:

    完整的解决方案。

        var gAssembly = Assembly.LoadFrom(JLDBConnection.Properties.Settings.Default.DefaultString + @"\JLRetailTerminal.exe");
        Type typ = gAssembly.GetExportedTypes().Where(s => s.Name == "RetailWindow").FirstOrDefault();
        //if extend sale is set prevent multiple windows
        if (gWindow == null)
        {//show if window has never been opened
              gWindow = (Window)Activator.CreateInstance(typ);
              gWindow.Show();
        }
        else
        {//window has been opened
              var windows = Application.Current.Windows; //get all opened windows in applications                                        
              if (!windows.OfType<Window>().Contains(gWindow) || !(JLDBConnection.Properties.Settings.Default.ExtendSale == "Yes"))
              {   //if window has been closed   or not multiple sale database                                                                                
                    gWindow = (Window)Activator.CreateInstance(typ);
                    gWindow.Show();
              }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多