我可以在Windows Phone 8中使用消息框在页面之间导航吗?

时间:2013-08-15 23:41:55

标签: c# windows-phone-8

基本上,在消息框中向用户显示一个问题,他们选择确定(是)或取消(否),然后根据他们的选择 - 然后导航到与他们的答案对应的下一页。 / p>

这是我不断得到的错误:“Microsoft.Phone.ni.dll中发生了'System.ArgumentException'类型的异常但未在用户代码中处理”

我尝试在MS网站上使用该示例,但每次都会崩溃我的应用程序。

这是我的XAML:

<phone:PhoneApplicationPage
    x:Class="Grub2._0.Hot"
    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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    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>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Foreground="Orange" Text="grub" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="Too hot to handle" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock FontSize="26" Foreground="Red" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="Auto" Width="Auto">
                Is it hot outside?
            </TextBlock>
            <Button Click="hotYes" Content="Yes" HorizontalAlignment="Left" Margin="0,45,0,0" VerticalAlignment="Top" Width="135"/>
            <Button Click="hotNo" Content="No" HorizontalAlignment="Left" Margin="140,45,0,0" VerticalAlignment="Top" Width="103"/>

        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

这是相应的c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Grub2._0
{
    public partial class Hot : PhoneApplicationPage
    {
        public Hot()
        {
            InitializeComponent();
        }

        private void hotYes(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("Parched.xaml", UriKind.Relative));
        }

        private void hotNo(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Is it currently 2-5 PM?",
                "Time", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                NavigationService.Navigate(new Uri("TacoBell.xaml", UriKind.Relative));
            }
            else if (result == MessageBoxResult.Cancel)
            {
                NavigationService.Navigate(new Uri("Pizza.xaml", UriKind.Relative));
            }

        }
    }
}

2 个答案:

答案 0 :(得分:3)

尝试在Uri中的页面名称前添加“/”,并确保页面名称的拼写正确(区分大小写)。

修改后的代码

private void hotNo(object sender, RoutedEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Is it currently 2-5 PM?",
            "Time", MessageBoxButton.OKCancel);

        if (result == MessageBoxResult.OK)
        {
            NavigationService.Navigate(new Uri("/TacoBell.xaml", UriKind.Relative));
        }
        else if (result == MessageBoxResult.Cancel)
        {
            NavigationService.Navigate(new Uri("/Pizza.xaml", UriKind.Relative));
        }

    }

答案 1 :(得分:0)

你可能想要添加&#34; /&#34;在Uri的链接字符串之前,因为Uri种类是相对的

喜欢:

NavigationService.Navigate(new Uri("/PageName.xaml", UriKind.Relative));
相关问题