Siliverlight 3 Navigation between user controls?

让人想犯罪 __ 提交于 2019-12-04 18:26:32

What I've usually done is to create a "MainPage.xaml" which is of type System.Windows.Controls.Navigation. That gets assigned to the RootVisual property of my application; it's pretty much empty, except for a navigation frame:

<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>

Then I use the "rootFrame" navigation frame to handle all my navigation needs, e.g., with these methods from a static NavigationManager class:

    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);
        }
    }

So in your case, you might use it like:

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

Or:

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

There are 3 types of containers in SL3

  1. Pages (Views)
  2. UserControls
  3. ChildWindows (Popups)

Do not swap UserControls, its a bad idea, it basically means clearing the "MainPage" content and Adding a new UserControl. By doing ths you lose the Back/Forth behavior of the browser since the URL never changes, thats not how the Navigation Framework was designed, you rather swap Pages (views) do this by using the 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));
    }

Now HomePage.xaml is a Page (not a UserControl), on Startup your default load page for the NavigationFrame should be your login page.

UserControls are meant to be re-usable functionality that can be deployed on multiple pages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!