C# 在某些条件下重定向到WPF中的另一页

C# 在某些条件下重定向到WPF中的另一页,c#,wpf,C#,Wpf,嗨,我正在申请WPF 我有一个主窗口,其中有我的导航,然后我还有一个stackpanel,其中有: <Frame x:Name="_mainFrame" NavigationUIVisibility="Hidden"/> 这很好,但现在我在那个窗口(gameWindow)中,我正在检查是否设置了“玩家”,如果没有,我想导航到另一个页面,在那里我可以设置某些值。 然后导航回GameWindow 但是,当大型机是主窗口的一部分时,我如何掌握它呢 它在大型机上的游戏窗口中显示 The n

嗨,我正在申请WPF

我有一个主窗口,其中有我的导航,然后我还有一个stackpanel,其中有:

<Frame x:Name="_mainFrame" NavigationUIVisibility="Hidden"/>
这很好,但现在我在那个窗口(gameWindow)中,我正在检查是否设置了“玩家”,如果没有,我想导航到另一个页面,在那里我可以设置某些值。 然后导航回GameWindow

但是,当大型机是主窗口的一部分时,我如何掌握它呢

它在大型机上的游戏窗口中显示

The name _mainFrame, does not exist in the current context
游戏窗口

public partial class GameWindow 
{
    private int numberOfPlayers;
    private Player[] players;

    private INavigator _navigator;

    public GameWindow(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 GameSettings());
        }
    }
}
 public partial class MainWindow : Window, 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 GameRulesWindow());
    }

    private void NavigateGameWindow(object sender, RoutedEventArgs e)
    {
        Navigate(new GameWindow(this));
    }
}
主窗口

public partial class GameWindow 
{
    private int numberOfPlayers;
    private Player[] players;

    private INavigator _navigator;

    public GameWindow(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 GameSettings());
        }
    }
}
 public partial class MainWindow : Window, 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 GameRulesWindow());
    }

    private void NavigateGameWindow(object sender, RoutedEventArgs e)
    {
        Navigate(new GameWindow(this));
    }
}
游戏设置

 public partial class GameSettings : Page
{
    public GameSettings()
    {
        InitializeComponent();

        //var gameProps = new GameProperties();

        // set number of players,, should prompt user, and get value!
        //gameProps.SetNumberOfPlayers(2);
    }
}
<Page x:Class="ITD.OOP_Projekt.WPF.Menu.GameSettings"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ITD.OOP_Projekt.WPF.Menu"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="GameSettings">

    <Grid Background="white">
        <TextBox HorizontalAlignment="Left" Height="23" Margin="229,144,0,0" TextWrapping="Wrap" Text="This is game settings" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Page>
查看游戏设置

 public partial class GameSettings : Page
{
    public GameSettings()
    {
        InitializeComponent();

        //var gameProps = new GameProperties();

        // set number of players,, should prompt user, and get value!
        //gameProps.SetNumberOfPlayers(2);
    }
}
<Page x:Class="ITD.OOP_Projekt.WPF.Menu.GameSettings"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ITD.OOP_Projekt.WPF.Menu"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="GameSettings">

    <Grid Background="white">
        <TextBox HorizontalAlignment="Left" Height="23" Margin="229,144,0,0" TextWrapping="Wrap" Text="This is game settings" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Page>

一个非常简单的解决方案是:

因此,使用下面的代码,您只有一个窗口(Mainwindow),并且在该窗口内显示页面。您可以将其与internet浏览器进行比较。您有一个窗口,在该窗口内,您可以在页面之间导航(设置、游戏、高分等)。 我希望这有帮助,你可以让它工作! 如果没有,我可以尝试在晚上上传一个简单的例子到github。 只要去掉你的游戏窗口,把它实现为一个页面

主窗口

public partial class GameWindow 
{
    private int numberOfPlayers;
    private Player[] players;

    private INavigator _navigator;

    public GameWindow(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 GameSettings());
        }
    }
}
 public partial class MainWindow : Window, 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 GameRulesWindow());
    }

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

第1页

xaml:

第2页

xaml:

这就是你真正需要的。 在本例中,您可以在按钮单击事件的两个页面之间切换。 只需启动一个新的wpf项目并复制代码。
反复使用它,直到您理解它,然后尝试在您的游戏中实现它:)

您从哪里得到这个错误?我的意思是代码的哪一部分?你应该有适当的和重要的代码块,这里没有人能理解你得到的确切问题。我要求共享所有必需的代码块。您获取名称的位置\u mainFrame在当前上下文中不存在error@GaurangDave当我尝试在大型机上使用与navigategamewindow相同的代码时,它就是这么说的。我认为至少有人应该提到这种方法是不寻常的。MVVM是WPF的事实标准,我建议您考虑一下。进行导航的一种简单方法是首先使用viewmodel。也。在wpf应用程序中,除了初学者之外,其他人很少使用页面。Contentcontrol和usercontrol在这方面更为常见。到目前为止,这个界面看起来很棒,我只是想尝试实现它,然后看看它是否有效。如果有效,我会将其设置为“已应答”。你能发布你的新代码吗?您的游戏窗口类和主窗口。我会尽力帮助你:)只需编辑你的问题添加了代码,我要展示的新窗口游戏设置也添加了视图,没有太多内容,只是一些文本,现在我将发布一个完整的示例项目
public partial class Page2 : Page
{
    private INavigator _navigator;

    public Page2(INavigator navigator)
    {
        _navigator = navigator;
        InitializeComponent();
    }

    private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
    {
        _navigator.Navigate(new Page1(_navigator  ));
    }
}