我的 WPF 应用程序中有一个MainWindow和一个user-control。我想调用我的函数MainWindowin user-control,没有creating new instance of MainWindow. 为此,我将主窗口设为用户控件的父级。我在下面写了这段代码来调用父函数。
子用户控制
public partial class AppLogo : UserControl
{
public MainWindow myparent { get; set; }
private void activate_Click_1(object sender, RoutedEventArgs e)
{
myparent.function();
}
. . .
}
父窗口:
public MainWindow()
{
InitializeComponent();
AppLogo childWindow = new AppLogo();
. . .
问题:
- 是否可以创建
Window父级user-control? - 如果上述问题的答案是
Yes为什么它会产生错误Object Reference is Null。 - 如果答案是
No it is not possible那么我该如何实现这个目标。因为有必要根据需要在我的应用程序中创建用户控件。