問題描述
我剛剛開始掌握來自 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 用戶控件.這將處理所有登錄,一旦通過身份驗證,我想導航到 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();
}
沒有運氣.我也嘗試過初始化一個新的 main 并設置它的可見性,但這當然不起作用.
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?
非常感謝.
編輯 - 進一步挖掘之后,這個 看起來像是一種可以做我所追求的方法,但它確實感覺有點駭人聽聞!這是 silverlight 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 用戶控件之間的導航?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!