Silverlight 3 用户控件之间的导航?

发布于 2024-08-15 03:37:14 字数 1016 浏览 1 评论 0原文

我刚刚开始掌握来自 ASP.NET 和 Flex 的 silverlight 3。

我已按照此处的新导航教程进行操作,并仔细阅读了身份验证和角色管理教程也。

所以,我有一个主页,其中有一个框架、网格内部和几个视图。这些都是可导航的并且工作正常。我将此主页视为我想要的小应用程序的主页。

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

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

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

我尝试过

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

但没有运气。我也尝试过初始化一个新的 main 并设置其可见性,但这当然不起作用。

我是否以正确的方式处理这个问题?

非常感谢。

编辑 - 好吧,进一步挖掘后,这个 看起来像是一种可以实现我所追求的方法,但它确实感觉有点黑客!这是 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.

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

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

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?

Thanks muchly.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-08-22 03:37:14

我通常做的是创建一个 System.Windows.Controls.Navigation 类型的“MainPage.xaml”。它被分配给我的应用程序的 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));

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));
流殇 2024-08-22 03:37:14

SL3 中有 3 种类型的容器

  1. 页面(视图)
  2. 用户控件
  3. 子窗口(弹出窗口)

不要交换用户控件,这是一个坏主意,它基本上意味着清除“MainPage”内容并添加新的用户控件。通过这样做,您将失去浏览器的后退/前进行为,因为 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 的默认加载页面应该是您的登录页面。

用户控件旨在成为可重用的功能,可以部署在多个页面上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文