【问题标题】:Why is my Frame waiting to navigate until another MessageBox appears?为什么我的 Frame 一直在等待导航,直到出现另一个 MessageBox?
【发布时间】: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,通知用户“保存完成”。以及返回到他们所在的上一页。

标签: c# wpf


【解决方案1】:

您不能在同一线程上同时睡觉、显示消息框和导航到页面。

如果您想在页面导航到之后显示MessageBox,您可以处理Navigated 事件。

此事件发生when the content that is being navigated to has been found, and is available from Content property, although it may not have completed loading:

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    MessageBoxResult messageBoxResult = MessageBox.Show("Navigate to page?", "Test", MessageBoxButton.YesNo);

    if (messageBoxResult == MessageBoxResult.Yes)
    {
        System.Windows.Navigation.NavigatedEventHandler handler = null;
        handler = async (ss, ee) =>
        {
            await Task.Delay(1000);
            MessageBox.Show("Success");
            MyFrame.Navigated -= handler;
        };
        MyFrame.Navigated += handler;
        MyFrame.NavigationService.Navigate(new Uri("Test.xaml", UriKind.Relative));
    }
}

【讨论】:

  • 我认为这对我需要做的事情非常有用,谢谢!我只需要将 Task.Delay(1000); 替换为我创建的任务,该任务执行我在显示测试页面时需要执行的所有其他操作。
【解决方案2】:

我遇到的问题是我的Frame 在显示“成功”MessageBox 之前没有更新。在搜索了一段时间后,我找到了一个 answer,其中提到了使用名为 Application.DoEvents() 的“黑客”。

但由于我使用的是 WPF 而不是 WinForms,所以没有 Application.DoEvents() 方法。相反,您需要调用

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));

在导航到您的页面之后和执行其他任务之前。 Here 是我找到的具体答案的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多