我需要访问承载给定控件的窗口(以下代码片段中的 this
)。
假设我的应用程序中只有一个窗口,下列哪个语句占用资源较少? (或者是否有更好的方法来做到这一点?)
Application.Current.MainWindow
Window.GetWindow(this)
最佳答案
有些人直到需要时才进行优化。无论如何,在这种情况下,资源或性能损失可能很小。 换句话说,您可能不必担心,您还有其他需要优化的东西。
这将返回或设置应用程序的主窗口:
// http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx
var w = Application.Current.MainWindow;
使用它来返回对控件所在窗口的引用:
// http://msdn.microsoft.com/library/vstudio/system.windows.window.getwindow.aspx
Window.GetWindow(theDependencyObject);
您说您需要访问承载给定控件的窗口。 那么我认为语义上更合适的是:
Window.GetWindow(theDependencyObject);
关于c# - Application.Current.MainWindow 与 Window.GetWindow(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939176/