简体   繁体   中英

Navigating pages not working unless on button click

I am developing a GUI application with WPF framework.

I have a Window, called MainWindow and a few pages. I have a menu with buttons, linked to different pages. With this kind of navigation I manage to navigate to different pages and come back home to MainWindow nicely. Example below.

How I am navigating with buttons - WORKING:

From Window (called MainWindow), to Page (called Settings)

private void btnSettings(object sender, RoutedEventArgs e)
    {
        Settings pg = new Settings();
        this.Content = pg;
    }

Going back from Page (called Settings), to Window (called MainWindow)

private void btnDomov(object sender, RoutedEventArgs e)
    {
        var MainWindow = new MainWindow();
        MainWindow.Show();
        Window parentWindow = Window.GetWindow(this);
        parentWindow.Close();
    }

The problem happens when I go to the page from MainWindow, but not with a button click, but as a result of some executed code (in my case when I read an RFID card, I go to "SkenirajKartico" page.). Actually I am able to come to the wanted page, but I am unable to go back to the MainWindow. When I click go to MainWindow (home) it refreshes the same page "SkenirajKartico". The code is shown below. Any Ideas how to solve this issue?

Example: After some code is executed, I am calling a method "pokliciDialog" with intention to go to wanted page ("SkenirajKartico")

How I am navigating after some code is executed - NOT WORKING

private void pokliciDialog()
    {
        if (startup == false)
        {
            SkenirajKartico pg = new SkenirajKartico();
            this.Content = pg;
        }
    }

As I said, page is refreshed to wanted page ("SkenirajKartico"). And when some code is executed on this page, I do the same step as above - Calling method "goToMain" to go back Home, but It refreshes current page "SkenirajKartico" and does not go to wanted MainWindow.

void goToMain()
{ 
    var MainWindow = new MainWindow();
    MainWindow.Show();
    Window parentWindow = Window.GetWindow(this);
    parentWindow.Close();
}

I see I am missing arguments, but if I add object sender and RoutedEventArgs e to the method, I don't know how to call them. Would be really happy if anyone can help me.

My bad, stupid mistake. I was calling method to show the page "SkenirajKartico" each time when MainWindow was loaded. That's why page "SkenirajKartico" was constantly refreshing. Wooops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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