问题描述:

I'm having the following Frame in SL4 app. What I want to do is to get a page object navigated inside of the frame to do unit testing (I don't need a URI).

<navigation:Frame Margin="0,0,0,0" JournalOwnership="OwnsJournal" Source="{Binding CurrentPage}">

<navigation:Frame.UriMapper>

<uriMapper:UriMapper>

<uriMapper:UriMapping Uri="/User" MappedUri="/Views/UserPage.xaml"/>

<uriMapper:UriMapping Uri="/Login" MappedUri="/Views/LoginPage.xaml"/>

<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>

</uriMapper:UriMapper>

</navigation:Frame.UriMapper>

Could anyone please tell me how to do that?

Thanks in advance,

yokyo

网友答案:

I found a solution after digging into System.Windows.Control.Navigation.dll with the Reflector for a while. There is no convenient properties to fulfill my requirements, but I can access to a navigated page object by a frame with the following code.

var page = _frame.Content as Page;  // Make sure to run this code after navigating the page

Nothing really is cool, but it works for me.

Cheers,
yokyo

网友答案:

I think I know what you mean. I was trying to make my main page detect what page was being navigated to for security purposes. Different users are allowed access to different pages, so if the user isn't allowed access to a page, but they type it into the URL bar they would be able to go there, so I wanted a central place where I could check what page they were going to against what they're allowed to go to. I did it this way. I added this to the end of ContentFrame_Navigated in MainPage.xaml.cs:

if (ContentFrame.Source.OriginalString.Contains("Tools"))
{
   ContentFrame.Navigate(new Uri("/Home", UriKind.Relative));
}

That worked for me. I had that inside the part where I checked whether the user was allowed to go to Tools.

相关阅读:
Top