无法通过单击按钮进行导航?

发布于 2024-10-08 02:21:21 字数 2299 浏览 2 评论 0原文

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

Following is my code.I am not able to navigate from MasterPage.xaml to Slide_show.xaml on a button click.

 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");
        }
    }

the xaml file is

<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>

The Slide_show.xaml.cs file is

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

       Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

垂暮老矣 2024-10-15 02:21:24

我看到 3 个问题。

第一个是,当导航到具有 Relative Uri 的页面时,您应该以 / 开头 uri。例如:

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

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

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

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

I see 3 issues.

The first is that when navigating to a page with a Relative Uri you should start the uri with a /. e.g:

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

The second is that Slide_show.xaml.cs is no defined as a partial class. In this case you are essentially defining 2 classes with the same name as a partial class will be generated based on the xaml. (Or rather the generated)

Thirdly, you are disabling the call to InitializeComponent(). Without this the page wouldn't be constructed correctly. (Assuming you'd addressed the last 2 issues.)

I'm guessing that you added the new page ("Slide_Show"). You then removed the partial keyword (for whatever reason) and then commented out the now invalid call to InitializeComponent.
Put back the code you deleted / commented out. The template puts it there for a reason.

万劫不复 2024-10-15 02:21:24

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

I think one "/" is missing in your Uri.
this.NavigationService.Navigate(new Uri("/Slide_show.xaml",UriKind.Relative));
nor try..

山川志 2024-10-15 02:21:24

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

Is an exception getting caught in your try/catch or is it just not displaying? If there is an exception what is it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文