by JBrooks
30. November 2010 08:57
Once the user was logged in I wanted to change the menu options that are shown. To do this I had to get a reference to MainPage. Here is the code for that:
MainPage m;
if (Application.Current.RootVisual.GetType().Name == "BusyIndicator")
m = (MainPage)((BusyIndicator)Application.Current.RootVisual).Content;
else
m = (MainPage)Application.Current.RootVisual;
The code below was a PREVIOUS TRY, but didn’t work because sometimes I would get an exception because the first Parent was null. Didn’t have time to look into this, and thought the code had some value so I left it here for now.
========= PREVIOUS TRY ============================================================
First I created a static method in the App class that walks up the hierarchy of parents until it finds a match. This can be used for more than just finding MainPage. Everything in the hierarchy should be derived from the FrameworkElement class.
public static FrameworkElement GetParentByName(FrameworkElement currentPage, string ParentName)
{
FrameworkElement fe = (FrameworkElement)currentPage.Parent;
// Walk your way up the chain of Parents until we get a match
while(fe.GetType().Name != ParentName)
fe = (FrameworkElement)fe.Parent;
return fe;
}
Then in the page Home I have the following code that calls that method.
private void Page_LayoutUpdated(object sender, System.EventArgs e)
{
if (WebContext.Current.User.IsAuthenticated)
{
MainPage m = (MainPage)App.GetParentByName(this, "MainPage");
m.LinkResetPassword.Visibility = System.Windows.Visibility.Collapsed;
m.LinkChangePassword.Visibility = System.Windows.Visibility.Visible;
}
}
6bfe2894-39a2-478c-a515-3055ee6958cd|2|3.5|27604f05-86ad-47ef-9e05-950bb762570c
Tags:
Silverlight