简体   繁体   中英

WPF page not showing content

i have a WPF application, where i have build a redirect if certain conditions are not met. It seems to redirect correctly, but it does not show the contents of the Xaml file..

It redirects from my GamePage to a GameSettingsPage.

The main window works as a master view, and the pages, i place inside a frame (_mainframe) which is inside a stackpanel, so i can switch out the contents without actually going completely away from the mainwindow.

Hope it makes sense, because after several hours, i have yet to find the issue why it will not show the game settings page...

my code:

Main Window

public partial class MainWindow : INavigator
{


    public MainWindow()
    {
        InitializeComponent();
    }

    private void ExitGame(object sender, RoutedEventArgs e)
    {
        System.Windows.Application.Current.Shutdown();
    }

    public void Navigate(Page p)
    {
        _mainFrame.Navigate(p);
    }

    private void NavigateRulesWindow(object sender, RoutedEventArgs e)
    {
        Navigate(new GameRulesPage());
    }

    private void NavigateGameWindow(object sender, RoutedEventArgs e)
    {
        Navigate(new GamePage(this));
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }
}

Game Page

public partial class GamePage : Page
{
    private int numberOfPlayers;
    private Player[] players;
    private int playerTurn = 0;
    private int diceThrow;
    private GameState state;
    private int delay = 500;
    private Dice dice = new Dice();
    private GameProperties _writeHelper;
    private GameRules _gameRules;

    private INavigator _navigator;

    public GamePage(INavigator navigator)
    {
        _navigator = navigator; //assign navigator so i can navigate _mainframe to other pages.

        // initialize game properties, check if they are set.
        var gameProp = new GameProperties();
        this.numberOfPlayers = 2;

        this.players = gameProp.CheckPlayerIsSet(this.players);
        //check if a player has been set
        if (this.players != null)
        { // Player is set or has been set. proceed or start the game.
            InitializeComponent();
        }
        else
        {   // redirect to settings window because players has not been set!
            _navigator.Navigate(new GameSettingsPage(_navigator));
        }
    }
}

Game Settings Page

public partial class GameSettingsPage : Page
{
    private INavigator _navigator;

    public GameSettingsPage(INavigator navigator)
    {
        InitializeComponent();
        _navigator = navigator; //assign navigator so i can navigate _mainframe to other pages.
    }
}

For good measure, my interface

public interface INavigator
{
    void Navigate(Page p);
}

You have to look at the order of execution, try placing a breakpoint into the Navigate(Page) method and you'll see what's happening.

To explain why nothing is displayed, let's look at what happens.

  1. You call the NavigateGameWindow() method.

  2. It starts constructing GamePage object.

  3. The condition goes into the else section.

  4. GameSettingsPage is constructed.

  5. _mainFrame.Navigate is called and _mainFrame navigates to the GameSettingsPage .

  6. Thread continues executing so nothing will be displayed yet.

  7. MainWindow.Navigate() returns back to the GamePage constructor , which is finished.

  8. Execution is now back in the NavigateGameWindow , which calls MainWindow.Navigate() again with the object of GamePage type.

  9. _mainFrame navigates to the GamePage object.

The reason nothing is displayed (and not the GamePage ) is that you don't call the InitializeComponents() in it's constructor when the condition isn't met.

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