c# - 单击按钮无法导航?

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

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

最佳答案

我看到 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 的无效调用。
放回您删除/注释掉的代码。模板将它放在那里是有原因的。

关于c# - 单击按钮无法导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4486935/

相关文章:

silverlight - 以编程方式使 Silverlight XAP 文件从浏览器缓存中过期

c# - 有没有办法在 Visual Studio 2010 中突出显示当前事件的代码块?

c# - 如何在 Silverlight 中显示 CCITT Group 4 TIFF 文件?

visual-studio-2010 - F#: curry 重载/成倍重载问题

c# - 在 C# 项目中添加引用...但它是 "goes away"?

c# - MVC Identity 2.0 和管理角色

c# - 如何计算 List<T> 中每个产品的最终价格?

c# - 简化条件if语句c#

c# - 是否可以在 OS X 中使用 WPF 和 C# 之类的东西?

wpf - 为什么我需要 SilverLight 中的 ContentPresenter?