Reputation: 61
i used vb.net before and want to explore c#. (they said c# is better than vb.net) but anyways... i used this code below from vb.net calling controls in parent window.
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
i found this code before in other forums and helps me a lot in vb.net... but now i want to use this code in c# for me to call controls... so i converted it using SharpDevelop ( a good software)....
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();
}
the problem is it gives me an error:
Error 1 Could not find an implementation of the query pattern for source type 'System.Windows.WindowCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 'w'.
Error #1 highlighted the Application.Current.Windows.
Error 2 A query body must end with a select clause or a group clause
Upvotes: 0
Views: 404
Reputation: 8503
I don't think you really need to use reflection here. You could try something much simpler:
var mainWindow = Application.Current.Windows.OfType<MainWindowForm>()
.FirstOrDefault();
if (mainWindow != null)
{
//Visibility stuff goes here
}
Upvotes: 1
Reputation: 34978
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()
Edit: Above should be the fully modified line. Added the space for the "where" condition and added the "select" statement.
A word of caution: Tools and samples are meant to aid understanding, they're not a substitute for it. Be sure to read and understand what the samples you come across, and tools generate/convert.
And C# is not "better" than VB.Net, it is a different language on the same platform. Of course, a lot of people will have the opinion it is better. :)
Upvotes: 0