Siliverlight 3 Navigation between user controls?

Im just starting to grab Silverlight 3 coming from ASP.NET and Flex.

I read the new navigation tutorial here and also read the authentication and role management guides.

So, I have a main page in which there is a frame, inside the grid and several views. They are all smooth and working fine. I see this main page as a kind of page for my little application that I'm talking about.

I know that I want to have user.xaml UserControl. This will handle all login after authentication. I want to go to MainPage and use its frame to go from there.

I just don’t want to just use the login as a separate page in my frame, since I want the login to use a different grid for the rest of the application, and also be separate.

So, how do I navigate from one user control (Login) to another (Main)?

I tried

 private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        //TO - DO: All the auth work, just want navigation sorted first

        this.Visibility = Visibility.Collapsed;
        App.Current.RootVisual = new MainPage(); 
    }

Bad luck. Ive also tried only init'n the new main one and set its visibility, but this of course does not work.

Am I even approaching this in the right way?

Thank.

Edit - Ok after digging a little further, this one looks like an approach that will do what it is after, but it feels a bit hack! Is this the suggested way for siverlight 3? Thanks again

+1
2

, "MainPage.xaml", System.Windows.Controls.Navigation. RootVisual ; , :

<navigation:Page 
x:Class="Client.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d" 
d:DesignWidth="400" 
d:DesignHeight="400" MinWidth="700" MinHeight="480"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Title="Main SlideLinc Page">
<Grid x:Name="LayoutRoot">
    <navigation:Frame x:Name="rootFrame" />
</Grid>
</navigation:Page>

"rootFrame" , , NavigationManager:

    public static void Navigate(string url, Action<Exception, UIElement> callback)
    {
        Navigate(new Uri(url, UriKind.RelativeOrAbsolute), callback);
    }

    public static void Navigate(Uri uri, Action<Exception, UIElement> callback)
    {
        if (rootFrame == null)
        {
            Logger.LogMessage("Can't use navigation, because rootFrame is null");
            ErrorMessageBox.Show(ClientStrings.NavigationFailed);
        }
        else
        {
            NavigatedEventHandler successHandler = null;
            NavigationFailedEventHandler failureHandler = null;
            successHandler = (s, e) =>
                 {
                     rootFrame.Navigated -= successHandler;
                     rootFrame.NavigationFailed -= failureHandler;
                     if (callback != null)
                     {
                         callback(null, e.Content as UIElement);
                     }
                 };
            failureHandler = (s, e) =>
                {
                    rootFrame.Navigated -= successHandler;
                    rootFrame.NavigationFailed -= failureHandler;
                    if (callback != null)
                    {
                        callback(e.Exception, null);
                    }
                };
            rootFrame.Navigated += successHandler;
            rootFrame.NavigationFailed += failureHandler;
            rootFrame.Navigate(uri);
        }
    }

, :

NavigationManager.Navigate(new Uri("/Login.xaml", UriKind.Relative), null);

:

NavigationManager.Navigate(new Uri("/Home.xaml", UriKind.Relative), (error, element) => InitializeElement(element));
+2

SL3 3

  • ()
  • UserControl
  • ChildWindows ( )

UserControls, , MainPage UserControl. , "/" , URL- , , , () NavigationService.

private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        //TO - DO: All the auth work, just want navigation sorted first

        NavigationService.Navigate(new Uri("/HomePage.xaml", UriKind.Relative));
    }

HomePage.xaml - ( UserControl), NavigationFrame .

UserControls , .

+2

Source: https://habr.com/ru/post/1726665/


All Articles