无法通过单击按钮进行导航?
以下是我的代码。单击按钮时无法从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到 3 个问题。
第一个是,当导航到具有
Relative
Uri 的页面时,您应该以/
开头 uri。例如:第二个是
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: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.
我认为您的 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..
您的 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?