I have a MainWindow which has a button that triggers a transition to the next UserControl:
private void Button_Click(object sender, RoutedEventArgs e)
{
NewPage newPage = new NewPage();
pageTransitionControl.ShowPage(newPage);
}
On this UserControl (NewPage), it has a back button which should transition the current UserControl away, so it goes back to MainWindow.
private void Back_Click(object sender, RoutedEventArgs e)
{
pageTransitionControl.UnloadPage();
}
The problem is that the back button on the UserControl, doesn't work unless it's on MainWindow e.g. The UserControl will unload if the back button is on the previous screen e.g. MainWindow in this case - And I can't figure out why this is?
public PageTransition()
{
InitializeComponent();
}
public void ShowPage(UserControl newPage)
{
currentPage = newPage;
if (contentPresenter.Content != null)
{
UnloadPage();
}
else
{
currentPage.Loaded += newPage_Loaded;
contentPresenter.Content = currentPage;
}
}
public void UnloadPage()
{
Storyboard hidePage = (Resources[string.Format("{0}Out", TransitionType.ToString())] as Storyboard).Clone();
hidePage.Completed += hidePage_Completed;
hidePage.Begin(contentPresenter);
}
void newPage_Loaded(object sender, RoutedEventArgs e)
{
Storyboard showNewPage = Resources[string.Format("{0}In", TransitionType.ToString())] as Storyboard;
showNewPage.Begin(contentPresenter);
}
void hidePage_Completed(object sender, EventArgs e)
{
contentPresenter.Content = null;
}