Siliverlight 3用户控件之间的导航? [英] Siliverlight 3 Navigation between user controls?

查看:61
本文介绍了Siliverlight 3用户控件之间的导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始接触来自ASP.NET和Flex的silverlight 3.

Im just starting to get to grips with silverlight 3, coming from ASP.NET and Flex.

我已在此处中阅读了新的导航教程,并仔细阅读了身份验证和角色管理教程.

I have followed the new navigation tutorial here and read through the authentication and role management tutorials also.

所以,我有一个主页,该主页在网格内部有一个框架和几个视图.这些都是可导航的并且可以正常工作.我认为这个主页就像是我所想到的小应用程序的母版页.

So, i have a main page, which has a frame, inside of the grid, and several views. These are all navigatable and working fine. I see this main page as kind of a master page to my little application i have i mind.

所以知道我想要一个login.xaml UserControl.这将处理所有登录,并且在通过身份验证后,我想导航到MainPage,并使用其框架从那里开始.

So know I want to have a login.xaml UserControl. This will handle all login and once authenticated I want to navigate to the MainPage, and the use its frame to go from there.

我不只是想简单地将登录名用作框架中的单独页面,因为我希望登录名与应用程序的其余部分使用不同的网格,并且也要分开.

I dont just want to simply use login as a seprate page within my frame as I want the login to use a different grid to the rest of the app, and also to be separate.

那么我将如何从一个用户控件(登录)导航到另一个用户控件(主要)?

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

我尝试过

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

没有运气. Ive还尝试了init'n一个新的main并设置了它的Visibility,但这当然行不通.

With no luck. Ive also tried just init'n a new main and setting its Visibility but this of course doesnt work.

我什至以正确的方式来处理这个问题吗?

Am I even approaching this in the correct way?

非常感谢.

编辑-进一步研究后,确定,看起来像一种方法,可以在即时消息之后执行,但确实有些破烂!这是siverlight 3的建议方法吗?再次感谢

Edit - Ok after digging a little further, this looks like an approach that will do what im after, but it does feel a little hackish! Is this the suggested way for siverlight 3? Thanks Again

推荐答案

我通常要做的是创建一个类型为System.Windows.Controls.Navigation的"MainPage.xaml".这将分配给我的应用程序的RootVisual属性;它几乎是空的,除了导航框:

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>

然后,我使用"rootFrame"导航框架来处理我的所有导航需求,例如,使用静态NavigationManager类中的以下方法:

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

或者:

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

这篇关于Siliverlight 3用户控件之间的导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆