Je suis dans un projet de test où I'have un MainWindow.xaml avec un cadre et deux boutons:WPF avec navigation Page NullReferenceException
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Name="button1" Click="button1_Click">button1</Button>
<Button Name="button2" Click="button2_Click">button2</Button>
<Frame Name="frame"></Frame>
</StackPanel>
</Window>
le Code est derrière ceci:
private void button1_Click(object sender, RoutedEventArgs e)
{
frame.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}
private void button2_Click(object sender, RoutedEventArgs e)
{
frame.Navigate(new Uri("Page2.xaml", UriKind.Relative));
}
Donc simplement. Evrey bouton ouvrir une page sur le cadre. Le problème est dans ce code:
public partial class Page1 : Page
{
public Page1()
{
this.Loaded += new RoutedEventHandler(CancelNavigationPage_Loaded);
this.Unloaded += new RoutedEventHandler(CancelNavigationPage_Unloaded);
InitializeComponent();
}
void CancelNavigationPage_Loaded(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
}
void CancelNavigationPage_Unloaded(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);
}
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
// Does the user really want to navigate to another page?
MessageBoxResult result;
result = MessageBox.Show("Do you want to leave this page?", "Navigation Request", MessageBoxButton.YesNo);
// If the user doesn't want to navigate away, cancel the navigation
if (result == MessageBoxResult.No) e.Cancel = true;
}
}
Lorsque je clique sur le bouton openong la page1, je veux un message d'alerte, et si je clique sur « Non » Je reste dans cette page sinon, je vais sur page2.But quand j'essaye d'aller dans page2 j'ai une NullReferenceException sur CancelNavigationPage_Unloaded void. Quelqu'un peut-il m'expliquer comment résoudre ce problème?
Merci à l'avance
EDIT: Je modifié ainsi:
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (this.NavigationService.CurrentSource==this.NavigationService.Source)
{
this.NavigationService.StopLoading();
return;
}
// Does the user really want to navigate to another page?
MessageBoxResult result;
result = MessageBox.Show("Do you want to leave this page?", "Navigation Request", MessageBoxButton.YesNo);
// If the user doesn't want to navigate away, cancel the navigation
if (result == MessageBoxResult.No)
e.Cancel = true;
else // Remove Handler
{
if (this.NavigationService != null)
this.NavigationService.Navigating -= new NavigatingCancelEventHandler(NavigationService_Navigating);
}
}
De cette façon, si je clique sur le bouton 2 fonctionne correctement, et si je clique sur button1, qui est en cours la même Page1, l'application ne me demande pas si je veux quitter la page.
Stacktrace, ligne d'erreur réelle? –
this.NavigationService.Navigating - = new NavigatingCancelEventHandler (NavigationService_Navigating); – AlessandroG