c# - 如何从 WPF 中的当前页面调用另一个页面?
问题描述
我有一个简单的系统,可以在多个页面之间切换。MainWindow
有一些重定向到页面的功能:
每个其他功能正在重定向到另一个页面。
private void BtnDebug_Click(object sender, RoutedEventArgs e)
{
FrContent.Content = new Page_Debug();
}
这很好用,因为所有这些函数都是从MainWindow
. 我也需要从Page
上面的系统不起作用的地方给他们打电话。
这是我尝试使用的一种方式:
private readonly MainWindow _mainWindow = new MainWindow();
private void BtnShowNotes_OnClick(object sender, RoutedEventArgs e)
{
_mainWindow.FrContent.Content = new Page_Notes();
}
问题是它没有显示 XAML 中的任何元素,尽管它调用了InitializeComponent()
函数。为什么它不像函数那样起作用MainWindow
?
解决方案
您正在创建 的新实例MainWindow
。您应该访问Frame
已经存在的窗口。您可以使用以下Application.Current.Windows
属性获得对此的引用:
private void BtnShowNotes_OnClick(object sender, RoutedEventArgs e)
{
MainWindow mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if (mw != null)
mw.FrContent.Content = new Page_Notes();
}
推荐阅读
- ios - 将 iOS Prod Push Auth Key 导出到 P12 以用于 Firebase 云消息传递
- python - 如何有效地生成一个新的数据框,该数据框是现有 df 上的逐行函数的结果
- flutter - 如何在颤动中为所有屏幕自动设置 BackButtonCustomize?
- python - 传递一个列表作为输入
- react-native - Lottie Splash Screen with Expo - 带有 Expo 的动画闪屏
- css - 为什么 Sass 编译器会返回带有“根样式表”的错误
- python - Python webscraping - 搜索 EAN 的谷歌结果(13 个字母的数字)
- swift - 从表单选择器中选择 CoreData 实体
- html - 如何在 CSS 中居中其父高度的 90% 的图像
- javascript - 如何处理用户同时尝试填充 Firebase 实时数据库中的有限“插槽”