简体   繁体   English

从子页面访问父窗口UI

[英]Access Parent Window UI from Child Page

I have a Page that is loaded inside a Window . 我有一个加载到Window内的Page When the user logs in I would like to show an Image in the Window , the Login method is however within the Page . 当用户登录时,我想在Window显示Image ,但是Login方法在Page I'm trying to call a method inside the Window , which is working for displaying a MessageBox but not displaying the Image . 我试图在Window内调用一个方法,该方法可用于显示MessageBox但不显示Image Here is some code; 这是一些代码;

MainWindow 主窗口

public void initUI()
{
    navigationFrame.Navigate(new Uri("View/MainPages/LoginPage.xaml", UriKind.Relative));
}

public void ShowSDCImage()
{
    sdcLogo.Visibility = Visibility.Visible;
    MessageBox.Show("Test"); // This is displayed
}

ChildPage 子页面

private void EnterPressed(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        if (UserAuthenticationService.AuthenticateUser(passwordBox.Password))
        {
            var lGs = new LoginService();
            var sqlServerCheck = new MySQLServerCheck();
            if (sqlServerCheck.ServerIsOnline())
            {
                MainWindow mw = new MainWindow();
                mw.ShowSDCImage();                    
                NavigationService.Navigate(new Uri("View/MainPages/DashboardPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Sorry, the server is offline. Please notify IT.");
            }
        }
        else
        {
            MessageBox.Show("Incorrect Password");
        }
        e.Handled = true;
    }

    if (e.Key == Key.Escape)
    {
        Application.Current.Shutdown();
    }
}

As you can see in the MainWindow I am trying to use 如您在MainWindow中所见,我正在尝试使用

MainWindow mw = new MainWindow(); mw.ShowSDCImage();

and although this would display the MessageBox , it does not display the Image . 尽管这将显示MessageBox ,但不会显示Image What am I doing wrong and how can I successfully access the MainWindow's Image ? 我在做什么错,如何成功访问MainWindow的Image

public static MainWindow W;

private void EnterPressed(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        if (UserAuthenticationService.AuthenticateUser(passwordBox.Password))
        {
            var lGs = new LoginService();
            var sqlServerCheck = new MySQLServerCheck();
            if (sqlServerCheck.ServerIsOnline())
            {
                W.ShowSDCImage();                    
                NavigationService.Navigate(new Uri("View/MainPages/DashboardPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Sorry, the server is offline. Please notify IT.");
            }
        }
        else
        {
            MessageBox.Show("Incorrect Password");
        }
        e.Handled = true;
    }

    if (e.Key == Key.Escape)
    {
        Application.Current.Shutdown();
    }
}

Use: 采用:

// Call this in your MainWindow() code:
public MainWindow()
{
    yourClassHere.W = this;
}

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

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