C# 参考 WPF 中的活动窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2038879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refer to active Window in WPF?
提问by pkain
How can I refer to active Window of WPF application in C#, using something like ActiveForm property in WinForms?
如何使用 WinForms 中的 ActiveForm 属性来引用 C# 中 WPF 应用程序的活动窗口?
采纳答案by Aviad P.
One possible way would be to scan the list of open windows in the application and check which one of them has IsActive = true
:
一种可能的方法是扫描应用程序中打开的窗口列表,并检查其中哪一个具有IsActive = true
:
Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
Not sure if there may be more than one active window if, for example, there's a modal dialog showing, in which case, the owner of the dialog and the dialog itself might be active.
不确定是否可能有多个活动窗口,例如,如果有一个模式对话框显示,在这种情况下,对话框的所有者和对话框本身可能处于活动状态。
回答by ghord
There is better way to do this using PInvoke. Aviads answer is not working all the time (there are some edge cases with dialogs).
使用 PInvoke 有更好的方法来做到这一点。Aviads 的回答并非一直有效(有一些对话的边缘情况)。
IntPtr active = GetActiveWindow();
ActiveWindow = Application.Current.Windows.OfType<Window>()
.SingleOrDefault(window => new WindowInteropHelper(window).Handle == active);
One must include following import first:
必须首先包含以下导入:
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
回答by Richard Aguirre
I have problems With this way "Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive);" specialy because I was building an aplication with a main Window then i had problems when the main window was selected. I resolve it creating this:
我对这种方式有问题“Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive);” 特别是因为我正在构建一个带有主窗口的应用程序,然后在选择主窗口时遇到了问题。我解决了这个问题:
In some base class or App.xaml.cs create this:
在某些基类或 App.xaml.cs 中创建:
public static Window ActivatedWindow {get;set;}
Then put in your base class deriving Window or all of your Window's Activate Event:
然后放入派生 Window 或所有 Window 的 Activate 事件的基类:
First Option - personal Window Base Class:
第一个选项 - 个人窗口基类:
public class MetroToolWindowBase
{
public MetroToolWindowBase()
{
Activated += new EventHandler(MakeActive);
}
private void MakeActive(object sender, EventArgs e)
{
App.ActivatedWindow= this;
}
}
Second Option- In Activated Event of Windows:
第二个选项 - 在 Windows 的激活事件中:
private void XWindow_Activated(object sender,EventArgs e)
{
App.ActivatedWindow= this;
}
回答by NutCracker
I know this is a bit old question but I think my answer could help someone.
我知道这是一个有点老的问题,但我认为我的回答可以帮助某人。
My problem was this: I had a WPF MVVM application and I needed to get my MainWindow
instance in the second view, i.e. second view model, in order to set the visibility of title bar button to visible
.
我的问题是这样的:我有一个 WPF MVVM 应用程序,我需要MainWindow
在第二个视图中获取我的实例,即第二个视图模型,以便将标题栏按钮的可见性设置为visible
.
This is my solution:
这是我的解决方案:
MainWindow window = (MyApp.MainWindow)App.Current.MainWindow;
window.btnSearch.Visibility = System.Windows.Visibility.Visible;
Hope this would help someone.
希望这会帮助某人。