【问题标题】:Access Parent Window UI from Child Page从子页面访问父窗口 UI
【发布时间】:2016-02-23 11:09:45
【问题描述】:

我有一个Page,它加载在Window 中。当用户登录时,我想在Window 中显示Image,但是登录方法在Page 中。我试图在Window 中调用一个方法,该方法用于显示MessageBox 但不显示Image。这是一些代码;

主窗口

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
}

子页面

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();
    }
}

正如您在 MainWindow 中看到的,我正在尝试使用

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

虽然这会显示MessageBox,但它不会显示Image。我做错了什么,如何成功访问 MainWindow 的Image

【问题讨论】:

    标签: c# wpf xaml window


    【解决方案1】:
    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();
        }
    }
    

    用途:

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

    【讨论】:

    • 我遇到的问题是我没有创建新的 ChildPage。我使用 NavigationFrame 并简单地导航到它。
    • 你能直接打电话给ShowSDCImage(); 而不是mw.ShowSDCImage(); 吗?
    • 还是不行,我也不热衷于使用静态变量,一定有更好的办法
    • 好的。 EnterPressedMainWindow 是否属于同一类?
    • EnterPressedChildPageMainWindowParentWindow
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多