【发布时间】:2019-05-31 05:06:24
【问题描述】:
在用户从显示的MessageBox 中选择“是”后,我试图导航到Page,但我的Frame 直到我显示另一个MessageBox 告诉用户才进行导航导航已完成。
我尝试在“成功”MessageBox 之后添加另一个Thread.Sleep(5000); 和MessageBox,(查看Frame 是否仅在整个方法完成后更新)但框架仍会尽快导航出现“成功”MessageBox。
这是我的 MainWindow.xaml:
<Window x:Class="WpfApp23.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp23"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10*" />
<RowDefinition />
</Grid.RowDefinitions>
<Frame x:Name="MyFrame" Content="" Margin="0" />
<Button x:Name="MyButton" Content="Navigate" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="75" Click="MyButton_Click" Grid.Row="1" />
</Grid>
</Window>
这是我在 MainWindow.xaml.cs 中的按钮单击事件处理程序:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResult = MessageBox.Show("Navigate to page?", "Test", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
MyFrame.NavigationService.Navigate(new Uri("Test.xaml", UriKind.Relative));
Thread.Sleep(5000); // Used to exaggerate time it takes to do other tasks.
MessageBox.Show("Success");
}
}
Test.xaml 只是我创建的一个虚拟页面,它只包含一个标签:
<Grid>
<Label Content="Test Page" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" />
</Grid>
我希望Frame 在用户选择“是”后立即导航,那为什么要等到“成功”MessageBox 出现才导航?
【问题讨论】:
-
您无法入睡,显示消息框并同时导航到同一线程上的页面。您想在页面导航到 之后显示 MessageBox 还是您想做什么?
-
我的意思是,你不能在 Test.xaml.cs 上添加 MessageBox.Show("Success") 吗?
-
我正在导航的页面只是一个填充页面,在应用程序等待完成某些任务时向用户显示。我最初的想法是在我的应用程序完成所有保存时仅向用户显示“正在保存 - 请稍候...”的页面。保存完成后,会出现另一个 MessageBox,通知用户“保存完成”。以及返回到他们所在的上一页。