在 WPF 中的页面和 Windows 之间导航
Navigation between Pages and Windows in WPF
我有一个问题想要解决。我有两个 WPF windows。那些 windows 包含包含内容的页面。我的问题不是逐页导航,而是 window 到 window。这是代码示例:
LoginPortal Design
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
</Grid>
LoginPortal Code
public LoginPortalMain()
{
InitializeComponent();
MainFrame.Navigate(new Uri("/Pages/LoginPortal.xaml", UriKind.RelativeOrAbsolute));
}
这是我的登录门户,有 4 个按钮。每个按钮都会打开第二个 window,其中包含多个页面。
private void BtnSales_Click(object sender, RoutedEventArgs e)
{
var w = Application.Current.Windows[0];
w.Hide();
MainWindow mn = new MainWindow();
mn.Content = new SalesPage();
mn.Show();
}
单击此按钮后,它会打开第二个 WPF Window,其中包含多个页面。导航没有问题。当我将关闭事件从 MainWindow 连接到 LoginPortalMain
时,问题就开始了
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
LoginPortalMain lgm = Application.Current.MainWindow as LoginPortalMain;
lgm.Show();
}
然后又回到起点。现在我的问题是如果我再次点击同一个按钮。它只是复制一切。然后在关闭任何东西时它只会添加新的 windows。如果我然后退出任何 window,它会在下面的图片中抛出一个错误。我的问题是,有没有办法在 MainWindow 关闭后处理导航。因此,一旦您第二次关闭 mainWindow,就不会有重复的 windows。我以前在单个 window 上使用过导航,但没有在多个 windows.
上使用过导航
我可能不得不重新考虑整个结构并使用自定义 UserControl 并弄清楚从 Pages 到 UserControl 的动画顺序。但是,我们将不胜感激。
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here
如果您只想打开新 window 的一个实例,请尝试显示和隐藏一个 window 实例。
当按下打开 window 按钮时,如果需要创建 window 并显示它。
private OtherWindow mOtherWindow;
private void OpenWindowButtonClick(object sender, RoutedEventArgs e)
{
mOtherWindow ??= new OtherWindow(); //initialize child window on first call
mOtherWindow.Show();
}
当另一个 window 关闭时隐藏它并取消关闭请求。您也可以在此处重置其他 window 状态,以便下次显示时使用。
private void OtherWindowClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
{
((Window)sender).Hide();
Application.Current.MainWindow.Activate();
e.Cancel = true;
}
我有一个问题想要解决。我有两个 WPF windows。那些 windows 包含包含内容的页面。我的问题不是逐页导航,而是 window 到 window。这是代码示例:
LoginPortal Design
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
</Grid>
LoginPortal Code
public LoginPortalMain()
{
InitializeComponent();
MainFrame.Navigate(new Uri("/Pages/LoginPortal.xaml", UriKind.RelativeOrAbsolute));
}
这是我的登录门户,有 4 个按钮。每个按钮都会打开第二个 window,其中包含多个页面。
private void BtnSales_Click(object sender, RoutedEventArgs e)
{
var w = Application.Current.Windows[0];
w.Hide();
MainWindow mn = new MainWindow();
mn.Content = new SalesPage();
mn.Show();
}
单击此按钮后,它会打开第二个 WPF Window,其中包含多个页面。导航没有问题。当我将关闭事件从 MainWindow 连接到 LoginPortalMain
时,问题就开始了 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
LoginPortalMain lgm = Application.Current.MainWindow as LoginPortalMain;
lgm.Show();
}
然后又回到起点。现在我的问题是如果我再次点击同一个按钮。它只是复制一切。然后在关闭任何东西时它只会添加新的 windows。如果我然后退出任何 window,它会在下面的图片中抛出一个错误。我的问题是,有没有办法在 MainWindow 关闭后处理导航。因此,一旦您第二次关闭 mainWindow,就不会有重复的 windows。我以前在单个 window 上使用过导航,但没有在多个 windows.
上使用过导航我可能不得不重新考虑整个结构并使用自定义 UserControl 并弄清楚从 Pages 到 UserControl 的动画顺序。但是,我们将不胜感激。
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here
如果您只想打开新 window 的一个实例,请尝试显示和隐藏一个 window 实例。
当按下打开 window 按钮时,如果需要创建 window 并显示它。
private OtherWindow mOtherWindow;
private void OpenWindowButtonClick(object sender, RoutedEventArgs e)
{
mOtherWindow ??= new OtherWindow(); //initialize child window on first call
mOtherWindow.Show();
}
当另一个 window 关闭时隐藏它并取消关闭请求。您也可以在此处重置其他 window 状态,以便下次显示时使用。
private void OtherWindowClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
{
((Window)sender).Hide();
Application.Current.MainWindow.Activate();
e.Cancel = true;
}