简体   繁体   English

DirectCast任何窗口,不仅是主窗口?

[英]DirectCast any window, not only main window?

I've been trying to edit variables in other windows like in VB.NET using DirectCast. 我一直在尝试使用DirectCast在其他窗口(如VB.NET)中编辑变量。 This seems to be working very well with the main window, as I use 当我使用时,这似乎在主窗口上运行得很好

Private Main As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

But, I am unable to find a way to use this with a window other than the main one. 但是,除了主窗口外,我无法找到一种在其他窗口上使用此方法的方法。 For now, I am stuck using this 现在,我被困在这个

Dim WindowOne As New Window1
WindowOne.Show()

This works, but I would rather not have to create a new instance of the window each time I want it to open. 这行得通,但我不想每次希望打开窗口时都创建一个新实例。 I have tried using 我尝试使用

Private WindowOne As Window1 = DirectCast(Application.Current.Windows.OfType(Of Window1).First(), Window1)

but it always gives me an error saying that "The sequence contains no elements". 但是它总是给我一个错误,说“序列不包含任何元素”。

Is there any other way to do this? 还有其他方法吗? What am I doing wrong? 我究竟做错了什么?

The correct syntax is below. 正确的语法如下。

Private WindowOne As Window1 = Application.Current.Windows.OfType(Of Window1)().FirstOrDefault()
If Not WindowOne Is Nothing Then
  'object is available here
End If

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

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