2009-11-12 112 views
1

我想導航到傳遞參數的頁面,我必須從主頁面開始工作,但是當我嘗試在較低級別上做類似的事情時,我得到的頁面未找到錯誤(索引頁面工作,但DetailsIndex沒有)Silverlight導航問題

MainPage.xaml中

<navigation:Frame.UriMapper> 
        <uriMapper:UriMapper x:Name="myUri"> 
        <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/> 
        <uriMapper:UriMapping Uri="DetailsIndex/{id}" MappedUri="/Views/DetailsIndex.xaml?id={id}"/> 
        <uriMapper:UriMapping Uri="Index/{id}" MappedUri="/Views/Index.xaml?id={id}"/> 
        <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/> 
        </uriMapper:UriMapper> 

MainPage.xaml.cs中(這工作)

this.ContentFrame.Source = new Uri("/index?id=19", UriKind.Relative); 

IndexPage.xaml.cs(這得到一個錯誤 - >頁面未找到: 「DetailsIndex ID = 66?」)

private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Button btn = sender as Button; 
      string id = btn.Tag.ToString(); 


     this.NavigationService.Navigate(new Uri(string.Format("DetailsIndex?id={0}", id), UriKind.Relative));    
    } 

回答

2

你應該使用URL,而不是映射的URI中導航。

this.NavigationService.Navigate(new Uri(string.Format("DetailsIndex/{0}", id), UriKind.Relative)); 

另外,在Uris的映射中,我相信他們通常以一個領先的/開頭。

+0

謝謝...它確實與「/」一起工作,我有一些非常奇怪的行爲,它是導航在頁面上,然後當我使用導航條件......這拋出了我的錯誤。 我也在討論使用單例模式而不是像這樣傳遞參數......它似乎有點笨拙 – 2009-11-12 23:42:57

0

做了一些調查後,我發現下面的解決方案,它爲我工作

的XAML:

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

代碼隱藏:

private void HyperlinkButton_Click(object sender, RoutedEventArgs e) 
{ 
    HyperlinkButton hyp = sender as HyperlinkButton; 
    string customer_id = hyp.Tag.ToString(); 
    this.NavigationService.Navigate(new Uri("/CustomerDetails?CustomerId=" + customer_id, UriKind.Relative)); 
} 

這樣我可以通過查詢字符串也

+1

我有點驚訝......使用該模式,您要求/ Views/CustomersDetails?CustomId = 1.xaml,它肯定不存在。也許我在這裏弄錯了,但我有這個確切的問題。 – jv42 2011-05-04 10:30:16