简体   繁体   English

来自MainWindow的WPF调用方法不起作用

[英]WPF call method from MainWindow not working

I have the following classes: 我有以下课程:

MainWindow: 主窗口:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        MainMenu mainMenu = new MainMenu(this);
        menuFrame.Navigate(mainMenu);
        UserPage userP = new UserPage();
        contentFrame.Navigate(userP);
    }

    public void LoadAPage(Page page)
    {
        contentFrame.Navigate(page);
    }
}

MainMenu: 主菜单:

public partial class MainMenu : Window
{
    private Window switchPage;

    public MainMenu(MainWindow mainP)
    {
        Window mainWindow = mainP;
        InitializeComponent();
        switchPage = mainP;
    }

    private void btn_navigate_user(object sender, EventArgs e)
    {
        UserPage userP = new UserPage(ServiceLogic);

        //switchPage = Window.GetWindow(PageSwitcher);
        //switchPage.LoadAPage(new UserPage());
    }

As you can see, I'm trying to use the LoadAPage -method from the MainWindow. 如您所见,我正在尝试使用MainWindow中的LoadAPage方法。 The MainMenu and UserPage are childs from the MainWindow. MainMenu和UserPage是MainWindow的子级。 The problem is, no matter what I try, I cannot reach the LoadAPage method. 问题是,无论我如何尝试,都无法达到LoadAPage方法。 I've tried setting the Owner but that doesn't work. 我尝试设置所有者,但这不起作用。 When trying mainMenu.Owner = this; 当尝试mainMenu.Owner = this; , Visual Studio says Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the method ,Visual Studio说Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the method Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the method LoadAPage` is unknown there. Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the method LoadAPage Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the method是未知的。

What am I doing wrong? 我究竟做错了什么? How should I solve this? 我该如何解决?

EDIT: Changing MainMenu to a Window instead of UserControl makes me able to set the Owner. 编辑:将MainMenu更改为窗口而不是UserControl使我能够设置所有者。 Still, I can't reach the method. 不过,我无法达到该方法。

What a mess :) Try this 真是一团糟:)试试这个

private MainWindow switchPage;

public MainMenu(MainWindow mainP)
{
    InitializeComponent();
    switchPage = mainP;
}

private void btn_navigate_user(object sender, EventArgs e)
{
    UserPage userP = new UserPage(ServiceLogic);
    switchPage.LoadAPage(userP);
}

However i think you ll be better off using events, instead of passing your window object to its childs. 但是我认为您最好使用事件,而不是将window对象传递给它的子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM