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

本文关键字:导航 消息 之间 Phone Windows 我可以 | 更新日期: 2023-09-27 18:27:48

基本上,用户会在消息框中收到一个问题,他们会选择"确定"(是)或"取消"(否),然后根据自己的选择,导航到与其答案对应的下一页。

这是我不断得到的错误:"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));
            }
        }
    }
}

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

尝试在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));
        }
    }

您可能需要在Uri的链接字符串之前添加"/",因为Uri类型是相对

类似:

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