I'm trying to navigate to a Page
after a user chooses "Yes" from a MessageBox
that is displayed, but my Frame
does not do the navigation until I display another MessageBox
telling the user that the navigation has completed.
I've tried adding another Thread.Sleep(5000);
and MessageBox
after the "Success" MessageBox
, (to see if the Frame
only updates after the entire method completes) but the Frame will still navigate as soon as the "Success" MessageBox
appears.
Here is my 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>
Here is my button click event handler from 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
is just a dummy page that I've created that holds nothing but a label:
<Grid>
<Label Content="Test Page" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" />
</Grid>
I would like the Frame
to navigate as soon as the user selected "Yes", so why is it waiting to navigate until the "Success" MessageBox
appears?