【问题标题】:Redirect to another page in WPF on certain conditions在某些条件下重定向到 WPF 中的另一个页面
【发布时间】:2018-04-30 08:38:40
【问题描述】:

您好,我正在制作 WPF 应用程序。

我有一个主窗口,其中有我的导航,然后我还有一个堆栈面板,其中:

<Frame x:Name="_mainFrame" NavigationUIVisibility="Hidden"/>

里面,我把我的页面放在里面。

在我的主窗口中,我使用以下命令导航到其他页面,例如移动到游戏窗口:

private void NavigateGameWindow(object sender, RoutedEventArgs e)
        {
            _mainFrame.Navigate(new GameWindow());
        }

这工作正常,但现在我在那个窗口(gameWindow)内,我正在检查是否设置了“玩家”,如果没有,我想导航到另一个页面,在那里我可以设置某些值。 然后导航回 GameWindow。

但是当 _mainFrame 是主窗口的一部分时,我如何获得它?

它在 _mainFrame 上的 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 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>

【问题讨论】:

  • 你在哪里得到这个错误?我的意思是代码的哪一部分?
  • 你应该正确和重要的代码块,这里没有人能理解你得到的确切问题。
  • 我要求分享所有必需的代码块。你得到的名字_mainFrame,在当前上下文中不存在这个错误
  • @GaurangDave 当我尝试使用与navigationgamewindow 相同的代码和_mainframe 时,它​​就是这么说的。
  • 我认为至少有人应该提到这种方法是不寻常的。 MVVM 是 WPF 的事实标准,我建议您考虑一下。一种简单的导航方法是首先使用 viewmodel。还。除了 wpf 应用程序的初学者外,很少有人使用页面。 Contentcontrol 和 usercontrol 会更常见。

标签: c# wpf


【解决方案1】:

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

因此,使用以下代码,您只有一个窗口(主窗口),并且在该窗口内显示您的页面。您可以将其与您的互联网浏览器进行比较。您有一个窗口,在该窗口内您可以在页面之间导航(设置、游戏、高分……)。 我希望这会有所帮助,你可以让它工作! 如果没有,我可以尝试在晚上将一个简单的示例上传到 github。 只需摆脱您的 GameWindow 并将其实现为页面即可。

主窗口

xaml:

<Window x:Class="PageSwitcher.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PageSwitcher"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>

  <Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />

</Grid>

cs:

public partial class MainWindow : INavigator
{
    public MainWindow()
    {
        InitializeComponent();
        Navigate( new Page1(this) );
    }

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

第1页

xaml:

<Page x:Class="PageSwitcher.Page1"
  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:PageSwitcher"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
  Title="Page1">

<Grid Background="Green">
  <Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page2" /> 
</Grid>

cs:

public partial class Page1 : Page
{

    private INavigator _navigator;

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

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

第2页

xaml:

<Page x:Class="PageSwitcher.Page2"
  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:PageSwitcher"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
  Title="Page2">

<Grid Background="Blue">
    <Button Width="150" Height="30" Click="ButtonBase_OnClick" Content="Go to Page1"/> 
</Grid>

cs:

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

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

【讨论】:

  • 到目前为止,这个界面看起来很棒,我只是想尝试实现它,然后看看它是否有效。如果可行,我会将其设置为已回答。
  • 你能发布你的新代码吗?请您的 GameWindow 类和 MainWindow。我会尽力帮助你:) 只需编辑你的问题
  • 添加了代码,以及我试图显示的新窗口游戏设置
  • 还添加了视图,不多说,暂时就一些文字
  • 我会发布一个完整的示例项目
【解决方案2】:

如果您想将页面从一个页面重定向到另一个页面,您可以创建一个方法或函数,您可以在其中编写条件并使用以下代码进行重定向。它也会处理目录。

NavigationService.Navigate(new Settings.LatestActiveUsers());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多