2010-12-20 64 views
2

以下是我的代碼。單擊按鈕時,我無法從MasterPage.xaml導航到Slide_show.xaml。無法導航按鈕點擊?

public partial class MainPage : PhoneApplicationPage 
{ public MainPage() 
    { 
     InitializeComponent(); 
     Loaded += new RoutedEventHandler(MainPage_Loaded); 


    } 
    private void Play_C(object sender, RoutedEventArgs e) 
    { 
     //Slide_show obj=new Slide_show(); 
     //obj.MainPage_Loaded(sender,e); 
     try 
     { 
      this.NavigationService.Navigate(new Uri("Slide_show.xaml",UriKind.Relative)); 

     } 
     catch (Exception e1) 
     { 
      MessageBox.Show("unable to show"); 
     } 
    } 

XAML文件是

<phone:PhoneApplicationPage xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
x:Class="photoViewer.MainPage" 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
shell:SystemTray.IsVisible="True"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
<Button Content="Play" Grid.Row="1" Height="72" HorizontalAlignment="Right" Margin="0,696,170,0" Name="button5" VerticalAlignment="Top" Width="114" Background="Transparent" Click="Play_C"/> 


</Grid> 

的Slide_show.xaml.cs文件

public class Slide_show : PhoneApplicationPage 
{ 
public Slide_show() 
    { 
     //InitializeComponent(); 

     Loaded += new RoutedEventHandler(MainPage_Loaded); 
    } 
    } 

回答

2

我看到3個問題。

第一個是,當導航到Relative Uri的頁面時,您應該啓動一個/的uri。例如:

NavigationService.Navigate(new Uri("/Slide_Show.xaml", UriKind.Relative)); 

第二個是Slide_show.xaml.cs沒有定義爲分部類。在這種情況下,您基本上定義了2個具有相同名稱的類,因此將根據xaml生成部分類。 (或者說生成的)

第三,您正在禁用對InitializeComponent()的調用。沒有這個頁面將無法正確構建。 (假設你已經解決了最後2個問題。)

我在猜測你添加了新的頁面(「Slide_Show」)。然後,您刪除了partial關鍵字(無論出於何種原因),然後將現在無效的調用註釋掉給InitializeComponent。
放回您刪除/註釋掉的代碼。該模板將其放在那裏是有原因的。

+0

感謝您的答覆..你知道我有代碼,但隨後評論它..科斯總是有錯誤說InitializeComponent()在當前上下文中找不到 – Shaireen 2010-12-20 10:39:23

2

我認爲一個 「/」 在您的烏里失蹤。 this.NavigationService.Navigate(new Uri(「/ Slide_show.xaml」,UriKind.Relative)); 也不試試..

+0

我曾嘗試這一點,但Slide_show.xaml是不可見的.. – Shaireen 2010-12-20 06:04:39

0

是一個異常陷入你的try/catch或它只是不顯示?如果有什麼例外,它是什麼?

+0

餘噸進入調試器斷裂App.xaml.cs並顯示導航失敗..私人無效RootFrame_NavigationFailed(對象發件人,NavigationFailedEventArgs E) { 如果(System.Diagnostics.Debugger.IsAttached) { //導航失敗;打入調試器 System.Diagnostics.Debugger.Break(); } } – Shaireen 2010-12-20 09:15:21

+0

你能從e.Exception中得到實際的異常嗎? – Fishcake 2010-12-20 09:38:43

+0

如何獲得?我試過e1.getBaseException(); ..但沒有碰巧 – Shaireen 2010-12-20 09:43:34