【问题标题】:Not able to navigate on button click?无法在按钮单击时导航?
【发布时间】:2010-12-20 04:45:09
【问题描述】:

以下是我的代码。我无法通过单击按钮从 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);
    }
  }

【问题讨论】:

    标签: c# silverlight visual-studio-2010 xaml windows-phone-7


    【解决方案1】:

    我看到了 3 个问题。

    首先,当导航到带有Relative Uri 的页面时,您应该以/ 开头。例如:

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

    第二个是Slide_show.xaml.cs 没有定义为部分类。在这种情况下,您实际上是在定义 2 个具有相同名称的类,因为将基于 xaml 生成部分类。 (或者更确切地说是生成的)

    第三,您正在禁用对InitializeComponent() 的调用。没有这个,页面将无法正确构建。 (假设您已经解决了最后两个问题。)

    我猜你添加了新页面(“Slide_Show”)。然后,您删除了 partial 关键字(无论出于何种原因),然后注释掉现在对 InitializeComponent 的无效调用。
    放回您删除/注释掉的代码。模板将其放在那里是有原因的。

    【讨论】:

    • 感谢您的回复.. 你说我有代码但后来评论了它..Coz 总是有一个错误说在当前上下文中找不到 InitializeComponent()
    【解决方案2】:

    我认为您的 Uri 中缺少一个“/”。 this.NavigationService.Navigate(new Uri("/Slide_show.xaml",UriKind.Relative)); 也不要尝试..

    【讨论】:

      【解决方案3】:

      您的 try/catch 中是否捕获了异常,或者它只是没有显示?如果有异常是什么?

      【讨论】:

      • 它进入 App.xaml.cs 中的调试器中断并显示导航失败.. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { / / 导航失败;闯入调试器 System.Diagnostics.Debugger.Break(); } }
      • 你能从e.Exception得到实际的异常吗?
      • 如何获得?我试过 e1.getBaseException(); ..但什么也没发生
      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多