繁体   English   中英

在父窗口中调用控件

[英]calling controls in Parent window

我以前使用vb.net并想探索C#。 (他们说c#比vb.net更好),但是无论如何...我在父窗口的vb.net调用控件中使用了以下代码。

Dim strWindowToLookFor As String = GetType(MainWindowForm).Name
Me.Close()
Dim win = (From w In Application.Current.Windows Where DirectCast(w, Window).GetType.Name = strWindowToLookFor Select w).FirstOrDefault
If win IsNot Nothing Then
    DirectCast(win, MainWindowForm).imglogin.Visibility = Windows.Visibility.Visible
    DirectCast(win, MainWindowForm).Focus()
End If

我之前在其他论坛上找到了此代码,并在vb.net中为我提供了很多帮助...但是现在我想在c#中使用此代码来调用控件...所以我使用SharpDevelop(一款出色的软件)对其进行了转换。 ...

        string strWindowToLookFor = typeof(MainWindowForm).Name;
        this.Close();
        var win = (from w in Application.Current.Windowswhere ((Window)w).GetType().Name == strWindowToLookForw).FirstOrDefault;
        if (win != null) {
            ((MainWindowForm)win).imglogin.Visibility = System.Windows.Visibility.Visible;
            ((MainWindowForm)win).Focus();
        }

问题是它给我一个错误:

错误1找不到源类型'System.Windows.WindowCollection'的查询模式的实现。 找不到“哪里”。 考虑明确指定范围变量“ w”的类型。

错误#1突出显示了Application.Current.Windows。

错误2查询正文必须以select子句或group子句结尾

我认为您真的不需要在这里使用反射。 您可以尝试更简单的方法:

var mainWindow = Application.Current.Windows.OfType<MainWindowForm>()
                                            .FirstOrDefault();

if (mainWindow != null)
{
   //Visibility stuff goes here
}
from w in Application.Current.Windowswhere

from w in Application.Current.Windows where



var win = (from w in Application.Current.Windows where ((Window)w).GetType().Name == strWindowToLookForw select w).FirstOrDefault()

编辑:上面应该是完全修改的行。 添加了“ where”条件的空间并添加了“ select”语句。

请注意:工具和示例旨在帮助理解,它们不能替代工具和示例。 确保阅读并了解遇到的示例以及工具生成/转换的内容。

C#并不比VB.Net“更好”,它是同一平台上的另一种语言。 当然,很多人都会认为它更好。 :)

暂无
暂无

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

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