簡體   English   中英

無法單擊按鈕進行導航?

[英]Not able to navigate on button click?

以下是我的代碼。單擊按鈕后,我無法從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);
    }
  }

我認為您的Uri中缺少一個“ /”。 this.NavigationService.Navigate(new Uri(“ / Slide_show.xaml”,UriKind.Relative)); 也不要嘗試

我看到3個問題。

第一個是在導航到帶有Relative Uri的頁面時,您應該以/開頭uri。 例如:

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

第二個是Slide_show.xaml.cs沒有定義為局部類。 在這種情況下,您實際上要定義2個與將基於xaml生成的部分類同名的類。 (或者更確切地說是生成的)

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

我猜您已添加了新頁面(“ Slide_Show”)。 然后,您刪除了partial關鍵字(無論出於何種原因),然后注釋掉了對InitializeComponent的無效調用。
放回您刪除/注釋掉的代碼。 該模板將其放在此處是有原因的。

是您的try / catch中捕獲了異常,還是只是不顯示異常? 如果有例外,那是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM